How to Build Web Barcode Apps with React and WebAssembly

Dynamsoft JavaScript barcode SDK is built based on WebAssembly, which provides high performance for reading barcodes in web apps. This tutorial aims to demonstrate how to quickly create a simple web barcode reader by using React and Dynamsoft JavaScript barcode SDK.



A Simple Web Barcode Reader

Create a new react app:

npx create-react-app react-wasm-barcode
cd react-wasm-barcode

Load and initialize the JavaScript barcode library in public/index.html:

<img src="loading.gif" style="margin-top:10px" id="anim-loading">
<script src=""></script>
  dynamsoft.dbrEnv.resourcesPath = '';
  dynamsoft.dbrEnv.onAutoLoadWasmSuccess = function () {
    window.reader = new dynamsoft.BarcodeReader();
    document.getElementById('anim-loading').style.display = 'none';
  dynamsoft.dbrEnv.onAutoLoadWasmError = function (ex) {
    document.getElementById('anim-loading').style.display = 'none';
    alert('Fail to load the wasm file.');
  dynamsoft.dbrEnv.licenseKey = "<Your Barcode License>";

If you do not have a valid license, you can register a Dynamsoft account and get a 30-day free trial license. To make the barcode reader globally accessible, assign the object to window.reader.

Create a barcode component in Barcode.js:

import React, { Component }from 'react';
export class Barcode extends Component {
    onChanged() {
        let image = document.getElementById('uploadImage').files[0];
        if (!image) {
            alert('Please add an image');
            var txts = [];
            for(var i=0;i<results.length;++i){
        }).catch(ex => {
            alert('error:' + (ex.message || ex));

    render() {
      return (
          <input id="uploadImage" type="file" accept="image/bmp,image/jpeg,image/png,image/gif" onChange={this.onChanged}/>

Use the Input element to load an image and write the barcode detection code in the onChanged() function.

Import the barcode component to App.js:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import {Barcode} from './Barcode';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />

export default App;

Run the app:

yarn start

Open localhost:3000 in your web browser to run the demo:

react web barcode reader

Source Code