How to Generate QR Code in ReactJS

Muhammad Usman Umar
3 min readFeb 28, 2024

--

Introduction

In today’s digital age, QR codes have become ubiquitous. From accessing websites to making payments, these square-shaped barcodes offer a convenient way to interact with digital content. If you’re a ReactJS developer looking to integrate QR code generation into your web application, you’re in luck! The qrcode.react library simplifies the process, allowing you to create QR codes effortlessly within your React components.

Getting Started with qrcode.react

qrcode.react is a lightweight and easy-to-use library for generating QR codes in React applications. To begin, make sure you have Node.js and npm installed on your development environment. Then, follow these simple steps:

  1. Install qrcode.react: You can install qrcode.react via npm or yarn by running the following command in your terminal:
npm install qrcode.react

or

yarn add qrcode.react

2. Import QRCode component: Once installed, import the QRCode component into your React component where you want to generate the QR code.

import QRCode from 'qrcode.react';

Generating QR Codes

Now that you have qrcode.react set up in your project, let’s dive into how you can generate QR codes dynamically based on user input or any other data source. Below is a basic example demonstrating how to generate a QR code with static data:

import React from 'react';
import QRCode from 'qrcode.react';

const QRCodeGenerator = () => {
return (
<div>
<h1>QR Code Generator</h1>
<QRCode value="https://example.com" />
</div>
);
};

export default QRCodeGenerator;

In this example, the QRCode component takes a value prop, which represents the content you want to encode into the QR code. Here, we're providing a URL, but you can pass any string data such as text, numbers, or even JSON objects.

Dynamic QR Code Generation

Generating QR codes dynamically based on user input is a common use case. Let’s enhance our example to allow users to enter their own text and generate a QR code accordingly:

import React, { useState } from 'react';
import QRCode from 'qrcode.react';

const DynamicQRCodeGenerator = () => {
const [inputValue, setInputValue] = useState('');

const handleChange = (e) => {
setInputValue(e.target.value);
};

return (
<div>
<h1>Dynamic QR Code Generator</h1>
<input type="text" value={inputValue} onChange={handleChange} placeholder="Enter text" />
{inputValue && <QRCode value={inputValue} />}
</div>
);
};

export default DynamicQRCodeGenerator;

In this updated example, we’ve added an input field where users can enter their desired text. The value prop of the QRCode component is now bound to the inputValue state, ensuring that the QR code updates in real-time as the user types.

Conclusion

Integrating QR code generation into your ReactJS applications has never been easier, thanks to the qrcode.react library. Whether you need to create static QR codes or generate them dynamically based on user input, this library provides a straightforward solution.

By following the steps outlined in this blog post, you can quickly incorporate QR code functionality into your React projects, opening up a world of possibilities for enhancing user experience and streamlining interactions with digital content. So why wait? Give it a try and start generating QR codes in ReactJS today!

--

--

Muhammad Usman Umar

Versatile Full Stack Developer with expertise in front-end and back-end technologies. Proven skills in creating dynamic and responsive web applications.