How to generate QR-Code using 'react-qr-code' in ReactJS ?
Last Updated :
25 Sep, 2024
react-qr-code is a module or package for react that provides the QRCode component to generate the code with custom values. QR code is a square grid that can easily be readable with digital devices like smartphones.
Prerequisites:
Approach
To generate QR code in react js we will use the react-qr-code npm package. We will the inputs from the users for value, background color, foreground colors, and size of qr code. Install the package as project dependency and use these values as given below:
Syntax:
<QRCode
title="title"
value="value"
bgColor="background-color"
fgcolor="foreground-color"
level="level"
size={number}
/>
PropTypes:
- value: It is the value of the Qr-code.
- title: It is the title of Qr-code.
- bgcolor: It is the background color of the Qr-code.
- fgcolor: It is the foreground color of the Qr-code.
- size: It is the size of the Qr-code.
- level: It defines the level of Qr-code.
Creating React Application and Installing Module:
Step 1: Create the React app using the command:
npx create-react-app gfg-qrcode
Step 2: Now move into your project folder i.e. gfg-qrcode by using this command:
cd gfg-qrcode
Step 3: Now install the package into your project folder using the following command:
npm i react-qr-code
Project Structure:
The updated dependencies after installing required module:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-qr-code": "^2.0.12",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example:This example implements a qr code generator using the react-qr-code module.
JavaScript
// Filename - App.js
import { useState } from 'react';
import QRCode from 'react-qr-code';
function App() {
const [value, setValue] = useState();
const [back, setBack] = useState('#FFFFFF');
const [fore, setFore] = useState('#000000');
const [size, setSize] = useState(256);
return (
<div className="App">
<center>
<br />
<br />
<input
type="text"
onChange={(e) => setValue(e.target.value)}
placeholder="Value of Qr-code"
/>
<br />
<br />
<input
type="text"
onChange={(e) => setBack(e.target.value)}
placeholder="Background color"
/>
<br />
<br />
<input
type="text"
onChange={(e) => setFore(e.target.value)}
placeholder="Foreground color"
/>
<br />
<br />
<input
type="number"
onChange={(e) => setSize(parseInt(e.target.value ===
'' ? 0 : e.target.value, 10))}
placeholder="Size of Qr-code"
/>
<br />
<br />
<br />
{value && (
<QRCode
title="GeeksForGeeks"
value={value}
bgColor={back}
fgColor={fore}
size={size === '' ? 0 : size}
/>
)}
</center>
</div>
);
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
Use Case Diagram - Unified Modeling Language (UML) A Use Case Diagram in Unified Modeling Language (UML) is a visual representation that illustrates the interactions between users (actors) and a system. It captures the functional requirements of a system, showing how different users engage with various use cases, or specific functionalities, within
9 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read