Reactjs Barcode Scanner Tutorial
This tutorial walks through how to build a PWA Reactjs app that can scan a barcode and display the scanned text.Step 1: Setup and Installation
Before beginning, please make sure you’ve successfully installed the Domo Apps CLI and followed the basic setup and installation instructions found here.
Step 2: Create a new Reactjs project
After setting up your local environment, please initialize a Reactjs project using domo’s template found in this article.
Step 3: Launch a Local Development Environment
Start the app using either
yarn start or npm start, depending which package manager you chose to generate your app. A new tab will open in your browser with this url http://localhost:3000/. Any change you do in the app will be shown here in real time.
Step 4: Setting up Progressive Web App (PWA) Features.
Here we will set up your app to be PWA ready, just follow the following steps.
-
Create a new file named
service-worker.jsin thepublicdirectory of your project. This file will contain the service worker logic. -
Add a script tag to register the service worker in your index.html file. Place this script tag inside the
<head>section. -
Edit your
manifest.jsonand add the PWA metadata.
Step 5: Creating the App component
These are the steps necessary to create your app component, including the barcode scanner part of it. You may subdivide the component into smaller components, but that will not be addressed in this tutorial.
- Open the new directory created by the template generator with Visual Studio Code.
-
Start by creating a new folder called
componentsthis is where we will be creating each of the apps components. In our case, we will only be using one component, App, but it`s good practice organizing the app directory. -
Create a new folder inside called
app. Here is where we will move ourApp.xxfiles.- a. Move all the files that has
App.in it to theappfolder. For example:App.js
- a. Move all the files that has
-
Inside
index.jschange the import ofAppto reflect the change in directory.- from
import App from './App';toimport App from './components/app/App';
- from
-
Now we will install a library that will take care of scanning and decoding the barcode.
yarn add html5-qrcodeornpm install html5-qrcode, depending on which package manager you have.
-
Import
Html5Qrcodemodule from the library in ourApp.jsxfile. this is done in the top of the file.-
import from
html5-qrcode;
-
import from
-
Also import other dependencies and css
-
Now we create the scanner component inside the
App.jsxfile. Lets start by removing all the previous unnecessary code and replace them. -
Lets create some local states to track scanner status and results
- html5QrCodeRef is used to store the instance. Its necessary for the library.
-
We create a
handleScan()function to handle the start and stoping of the scanner.Inside the function we are calling various methods from the library, such as.startand.stop. We are also telling the library what the camera parameters will be, and also the size of the scanner zone. We are checking if the scanner is not running and that we have the instance (where the scanner will be displayed) in the DOM, before we start the library. If this is true, then we start the scanner and save the decodedText in the local state. If not, the app will try to stop the scanner. -
This useEffect was created to find the instance where the scanner will be displayed in the DOM. We will call it
readerin the render part of the code. -
Finally, the render is added. Here we will add the rendered parts of the app and style them. We require a
divwith a specific id so we can instance it, namedreader. All you need is the scanner div, and a button. Everything else is extra. You may style it as you like. -
Export the component to be accessed from
index.js. -
Create a
.envfile inside the main directory and add this code to it. Without it, the library may throw errors.
Extra information
Here are some additional files and information. What CSS styles was used and the whole
app component will be here, as well as the file directory, for easier navigation and reference.