diff --git a/README.md b/README.md index 8465639..7354d08 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Working on adding support for more and more platforms. If you find a platform or | Chrome
Chrome | Firefox
Firefox | Edge
Edge | Opera
Opera | Opera-Mini
Opera Mini | UC
UC | --------- | --------- | --------- | --------- | --------- | --------- | |![](./assets/done.png)| ![](assets/done.png)| ![](assets/done.png)| ![](assets/done.png)| ![](assets/partial.png) | ![](assets/partial.png) - + ### IOS | Safari
Safari | Chrome
Chrome | Firefox
Firefox | Edge
Edge @@ -43,9 +43,9 @@ Working on adding support for more and more platforms. If you find a platform or ### Framework support The library can be easily used with several other frameworks, I have been adding examples for a few of them and would continue to add more. -|| | -| -------- | -------- | -------- | -| [Html5](./examples/html5) | [VueJs](./examples/vuejs) | [ElectronJs](./examples/electron) +|| | | +| -------- | -------- | -------- | -------- | +| [Html5](./examples/html5) | [VueJs](./examples/vuejs) | [ElectronJs](./examples/electron) | [React](./examples/react) ## Description - [View Demo](https://blog.minhazav.dev/research/html5-qrcode.html) diff --git a/assets/react.svg b/assets/react.svg new file mode 100644 index 0000000..ea77a61 --- /dev/null +++ b/assets/react.svg @@ -0,0 +1,9 @@ + + React Logo + + + + + + + diff --git a/examples/react/README.md b/examples/react/README.md new file mode 100644 index 0000000..f5c4879 --- /dev/null +++ b/examples/react/README.md @@ -0,0 +1,89 @@ +# html5-qrcode with React +
+[reactjs.org](https://reactjs.org/) | `Support Level` = `Strong` + +## How to build a `React Plugin / Component` using `html5-qrcode` +We shall be using React's recommendation on [Integrating with Other Libraries](https://reactjs.org/docs/integrating-with-other-libraries.html) to create a plugin for `React`. + + +### Download the latest library +You can download this from [Github release page](https://github.com/mebjas/html5-qrcode/releases) or [npm](https://www.npmjs.com/package/html5-qrcode). And include this in `index.html`. + +```html + +``` + +### Create a new component `Html5QrcodeScannerPlugin` +You can write a custom plugin like this: + +```js +class Html5QrcodeScannerPlugin extends React.Component { + componentDidMount() { + // Creates the configuration object for Html5QrcodeScanner. + function createConfig(props) { + var config = {}; + if (props.fps) { + config.fps = props.fps; + } + if (props.qrBox) { + config.qrBox = props.qrBox; + } + if (props.aspectRatio) { + config.aspectRatio = props.aspectRatio; + } + if (props.disableFlip !== undefined) { + config.disableFlip = props.disableFlip; + } + return config; + } + + var config = createConfig(this.props); + var verbose = this.props.verbose === true; + + // Suceess callback is required. + if (!(this.props.qrCodeSuccessCallback )) { + throw 'qrCodeSuccessCallback is required callback.'; + } + + this.html5QrcodeScanner = new Html5QrcodeScanner( + 'qr-code-full-region', config, verbose); + this.html5QrcodeScanner.render( + this.props.qrCodeSuccessCallback, this.props.qrCodeErrorCallback); + } + + componentWillUnmount() { + // TODO(mebjas): See if there is a better way to handle + // promise in `componentWillUnmount`. + this.html5QrcodeScanner.clear().catch(error => { + console.error('Failed to clear html5QrcodeScanner. ', error); + }); + } + + render() { + return
; + } +} +``` + +### Use this new component in your React app +A very crude example would be to +```js +ReactDOM.render( +
+

Html5Qrcode React example!

+ { console.log(mesg); }} + qrCodeErrorCallback={error => { console.error(error); }} /> +
, + document.getElementById('root') +); +``` + +### Example implementation +You can find an example impelementation at [example.html](./example.html). + +### TODO(mebjas): Create and publish `Html5QrcodeScanner` as proper react plugin +It'd be great to publish this as a proper React plugin so every developer doesn't have to write custom React plugin to use this library. diff --git a/examples/react/example.html b/examples/react/example.html new file mode 100644 index 0000000..1921d86 --- /dev/null +++ b/examples/react/example.html @@ -0,0 +1,78 @@ + + + + + Hello World + + + + + + + + +
+ + +