Create a QR code generator app using ReactJS
Last Updated :
25 Jul, 2024
In this article, we will create a simple QR Code generator app. A QR code is a two-dimensional barcode that can be read by smartphones. It allows the encoding of more than 4,000 characters in a compact format. QR codes can be used for various purposes, such as displaying text to users, opening URLs, storing contact information in an address book, or sending messages.
Prerequisites:
Approach:
Our app consists of two sections. In the first section, we will collect user inputs such as the text to encode, the size of the QR code, and the background color of the QR code, and store these inputs in state variables. Subsequently, we will construct the necessary API string to fetch the QR code image. In the second section, we will display the generated QR code.
Creating a React application:
Step 1: Create a react application by typing the following command in the terminal.
npx create-react-app qrcode-gen
Step 2: Now, go to the project folder i.e qrcode.gen by running the following command.
cd qrcode-gen
Project Structure:.

Example: Here App.js is the only default component of our app that contains all the logic. We will be using a free opensource (no auth requires) API called ‘create-qr-code’ to fetch the required QR code image. We will also have a button to download the QR code image.
Now write down the following code in the App.js file.
JavaScript
import { useEffect, useState } from 'react';
import './App.css';
function App() {
const [temp, setTemp] = useState("");
const [word, setWord] = useState("");
const [size, setSize] = useState(400);
const [bgColor, setBgColor] = useState("ffffff");
const [qrCode, setQrCode] = useState("");
// Changing the URL only when the user
// changes the input
useEffect(() => {
setQrCode
(`http://api.qrserver.com/v1/create-qr-code/?data=${word}!&size=${size}x${size}&bgcolor=${bgColor}`);
}, [word, size, bgColor]);
// Updating the input word when user
// click on the generate button
function handleClick() {
setWord(temp);
}
return (
<div className="App">
<h1>QR Code Generator</h1>
<div className="input-box">
<div className="gen">
<input type="text" onChange={
(e) => {setTemp(e.target.value)}}
placeholder="Enter text to encode" />
<button className="button"
onClick={handleClick}>
Generate
</button>
</div>
<div className="extra">
<h5>Background Color:</h5>
<input type="color" onChange={(e) =>
{ setBgColor(e.target.value.substring(1)) }} />
<h5>Dimension:</h5>
<input type="range" min="200" max="600"
value={size} onChange={(e) =>
{setSize(e.target.value)}} />
</div>
</div>
<div className="output-box">
<img src={qrCode} alt="" />
<a href={qrCode} download="QRCode">
<button type="button">Download</button>
</a>
</div>
</div>
);
}
export default App;
Now, let's edit the file named App.css to design our app.
CSS
@import url('http://fonts.cdnfonts.com/css/lilly');
.App{
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 50px;
padding-top: 30px;
}
h1{
font-family: 'Lilly', sans-serif;
font-size: 50px;
}
.gen input{
height: 35px;
width: 250px;
font-size: 20px;
padding-left: 5px;
}
button{
position: relative;
height: 38px;
width: 100px;
top: -2px;
font-size: 18px;
border: none;
color: whitesmoke;
background-color: forestgreen;
box-shadow: 2px 2px 5px rgb(74, 182, 74);
cursor: pointer;
}
button:active{
box-shadow: none;
}
.extra{
padding-top: 20px;
display: flex;
justify-content: space-around;
gap: 10px;
}
.output-box{
display: flex;
flex-direction: column;
align-items: center;
gap: 40px;
}
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:
Create a QR code generator app using ReactJS
Similar Reads
Create a meme generator by using ReactJS In this tutorial, weâll create a meme generator using ReactJS. In the meme generator, we have two text fields in which we enter the first text and last text. After writing the text when we click the Gen button, it creates a meme with an image and the text written on it. Preview Image: meme generator
3 min read
Create an E-commerce App using React-Native An E-commerce app using react native is an application designed to facilitate online buying and selling of goods and services. These apps aim to provide a digital platform for businesses to showcase their products or services and for consumers to browse, compare, and purchase items without the need
12 min read
Create a Camera App using React-Native A camera app using react native is a mobile application that is designed to capture photos or videos using the built-in camera functionality of a mobile device. In this article, you will learn how you can create a camera app with simple steps.Output Preview: Let us have a look at how the final appli
3 min read
Build a Captcha Generator Using ReactJs A CAPTCHA generator is a tool that creates random and visually distorted text, requiring user input to prove they are human. It prevents automated bots from accessing websites or services by testing human comprehension. Our Captcha generator geÂnerates random text-baseÂd captchas that users must acc
4 min read
Create a Chatbot App using React-Native Creating a chatbot app using React Native will be an exciting project. In this article, we are going to implement a Chatbot App using React Native. Chatbot App is a mobile application that answers the user's questions on the basis of their previous learning or content provided by developers. It help
4 min read
Create Jokes Generator App using React-Native In this article, we are going to build a joke generator app using react native. React Native enables you to master the designing an elegant and dynamic useÂr interface while eÂffortlessly retrieving jokeÂs from external APIs. To give you a better idea of what weâre going to create, letâs watch a de
8 min read