How to build a QR Code generator using React

This utility app allows you to create your own QR code. You can build it easily, using the powerful QRCode library.

This utility app allows you to create your own QR code . You can build it easily, using the powerful QRCode library.

How to build a QR Code generator using React Picture 1How to build a QR Code generator using React Picture 1

QR Code (Quick Response) is a two-dimensional barcode that can store and transmit information in a machine-readable format. Such information may include links to company web sites, restaurant menus, product details or instruction pages.

QR code technology aims to help businesses provide users with quick access to product-related information. This can increase competitive advantage.

Here's how to make a QR code generator in React with a practical example .

Before start

You need:

  1. Basic knowledge of HTML input components, JavaScript and useState React hooks.
  2. Understand how to implement conditional rendering in React.
  3. Installed Node on local machine and know how to use package manager (npm or yarn).

What is QR Code Library?

QRCode is a third party library used to generate 2D barcodes. Currently QRCode is the most popular QR code library, with more than 1 million weekly installs and support for client & server applications.

 

To install QRCode in an existing React application, open a terminal, navigate to the project directory and run the following code:

yarn add qrcode

Or if you prefer npm, use:

yarn add qrcode

Project setting

The project's repository contains 2 branches: starter and final . You can use the starter as a base to build your project or the finale to preview the finished trial.

Clone the Starter app

Open a terminal and run the following command to clone the starter branch of the repository to the local machine:

git clone -b starter https://github.com/TipsMakecode/qr-code-generator.git

Run the following terminal command, in the project directory to install the dependencies:

Yarn

Or:

npm install

To start the application, run:

yarn start

Or:

npm start

Great, you will see the UI appear on the browser:

How to build a QR Code generator using React Picture 2How to build a QR Code generator using React Picture 2

Implement logic in steps

Open the file src/App.jsx and replace its contents with the following code:

import { useState } from "react"; import "./styles.scss"; import QRCode from "qrcode"; const App = () => { const [url, setUrl] = useState(""); const [dataUrl, setDataUrl] = useState(""); const handleQRCodeGeneration = () => { QRCode.toDataURL(url, { width: 300 }, (err, dataUrl) => { if (err) console.error(err); // set dataUrl state to dataUrl setDataUrl(dataUrl); }); }; return (

QR Code Generator

 setUrl(e.target.value)} /> { /* code to conditionally display the QR code and a download button would go here */ } ); }; export default App;

 

The code will run like this:

  1. Enter the QRCode at the top level.
  2. Generate url state to store entered URL and dataUrl state to contain generated QR code in DataURL form .
  3. Create the arrow function handleQRCodeGeneration , while it calls the toDataURL method of the QRCode object with the following arguments:
    1. The first is the URL to encode
    2. The second is an options object that defines the width of the QR code
    3. The third is a callback function that returns the dataURL version of the URL entered.
  4. Call the handleQRCodeGeneration arrow function in the form element when submitting the form.
  5. Update url status for user input value.

You can now show a conditionally generated view in a div element when the dataUrl state changes:

{dataUrl && ( qr code 

Run the application in the browser and confirm the results:

How to build a QR Code generator using React Picture 3How to build a QR Code generator using React Picture 3

Good luck!

5 ★ | 1 Vote