Implementation of QR Code Generator

Step-by-Step Implementation

In this lesson, we will get our hands dirty with the actual implementation of a QR code generator application in React JS. Let’s get started with the initial project setup!

Set up the environment

As the first step to get started with the implementation, we need to set up the environment for the project. Follow the below steps to set up the project.

  • Open VScode
  • Create a new folder named ‘qr-generator’
  • Open ‘new terminal’
  • Enter the command ‘npx create-react-app app’. This will create a new React JS project with the name ‘app’.
  • Once the installation is complete, enter the command ‘cd app & npm start’ to start the React JS application in localhost:3000.

After removing the unnecessary files and folders, the app directory will look like this.

Create Application Structure

In this step, we will create the basic structure to the application such as input and output display area. This will make it easy to view the elements we need for the application.

  • For the QR generator, we need one display area to display the generated QR code under the input area.
  • As the first stage, create a components folder in the src directory and create a file with the name ‘QrGenerator.jsx’

  • Now, copy and paste the code to create the basic structure of the application.
import QRCode from "qrcode";
// QrGenerator.jsx 


// Functional Component to Create the QR code generator 
function QrGenerator() {
  return (
    // The main container 
    <div>
    // Input container 
      <div>
           // Input field 
        <input
          type="text"
          id="input"
          placeholder="Eg: https://google.com/ "
   />
        // Generate QR code button
        <button>
          Generate
        </button>
      </div>
       // Display area
      <div>
        // Qr code display in image format
        <img src="/" alt="QR code" id="qr" />
         // Download QR code button
        <a href="/" download="qrcode.png">Download QR Code</a>
      </div>
    </div>
  );
}


export default QrGenerator;
  • After the QrGenerator component export, you need to import the component in the main component ‘App.js’ since it is the entry point for your application.
  • Modify the code in ‘App.js’ file similar to the below code.
// App.js


// Import the QrGenerator component 
import QrGenerator from "./components/QrGenerator";
function App() {
  return (
    <div>
      <QrGenerator />
    </div>
  );
}


export default App;
  • After updating the App component, you will see the following result.

Apply Styles to the Application

The next step is to add styles to each element to make it visually appealing using CSS. We can create n styling files one for each component. In this project, we will create two css files, one for the QrGenerator component and another for the App component.

  • Modify the QrGenerator component to add styling for each element. Using the ‘className’ attribute, we can match the styling to the element. Make sure to import the style file into the component.
// QrGenerator.jsx


import "./QrGenerator.css";


function QrGenerator() {
  return (
    <div className="qr-generator">
      <div className="input-container">
        <input
          type="text"
          id="input"
          placeholder="Eg: https://google.com/ "
        />
        <button className="submit">
          Generate
        </button>
      </div>
      <div className="display-container">
        <img src="/" alt="QR code" id="qr" />


        <a className="download-btn" href={qrCode} download="qrcode.png">Download QR Code</a>
      </div>
    </div>
  );
}

export default QrGenerator;
  • The next step is to create a styling file for QrGenerator. Create a file in the component folder ‘QrGenerator.css’. And write styles for each element.
/* QrGenerator.css */


/* Applying style for the body */
body{
    background-color: rgb(1, 46, 66);
    color: white;
}


/* Main container for QR generator */
.qr-generator{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
}


/* Input container */
.input-container{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 2rem;
    padding: 10px;
    margin: 35px 5px;
}


/* Input[text] field */
#input{
    width: 25rem;
    height: 50px;
    padding: 1rem;
    border-radius: 15px;
    border: 2px solid rgb(0, 0, 0);
}


/* QR generate button */
.submit{
    width: max-content;
    height: 50px;
    background: none;
    border: none;
    font-size: 1.5rem;
    font-weight: bold;
    color: white;
    cursor: pointer;
}


/* Hover style for generate button */
.submit:hover{
    color: rgb(83, 251, 12);
}


/* Display container */
.display-container{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2rem;
}


/* QR code image */
.qr{
    height: 5rem;
    width: 5rem;
}


/* Download button */
.download-btn{
    width: max-content;
    text-decoration: none;
    color: rgb(22, 203, 34);
    align-content: center;
    padding: 10px;
    border-radius: 25px;
    height: 50px;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
}


/* Download button hover feature */
.download-btn:hover{
    color: white;
}
  • Now, let’s add some styling to the App component.
// App.js
import "./App.css";
import QrGenerator from "./components/QrGenerator";
function App() {
  return (
    <div className="App">
      <h1>QR Code Generator</h1>
      <QrGenerator />
      <a href="https://www.guvi.in/" className="copywright">
        Made by Guvi💚
      </a>
    </div>
  );
}


export default App;
  • Modify the App.css file to include new stylings.
/* App.css */


*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: Georgia;
}


.App{
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
}


.copywright{
  color: white;
  text-decoration: none;
  font-size: 1rem;
  margin-top: 20px;
  top: 0%;
  bottom: 100%;
}
  • Once you add the styling, the application will look like the below output.

Add Functionality to the Application

The last part of the application is to add the logic for the generate button to be able to generate QR code and display in the output area. To do that, you do not need to create a separate file. We can achieve the logic by using useState(), onClicke() and onChange() functions we saw in the previous lesson.

Note: If you get any errors in importing the qrcode module, try installing manually in the terminal using ‘npm i qrcode’ command.

  • Add the logic to the QrGenerator component.
// QrGenerator.jsx


// Import the functions from the react package
import QRCode from "qrcode";
import { useState } from "react";
import "./QrGenerator.css";


function QrGenerator() {
  // Create variables to track the changes in input and qrcode.
  const [input, setInput] = useState("");
  const [qrCode, setQrCode] = useState("");


  // Method to handle the changes in the input field
  const handleInputChange = (e) => {
    setInput(e.target.value);
  };


  // Method to generate QR code
  const generateQRCode = () => {
    // toDataURL() from qrcode converts website link into QR code
    QRCode.toDataURL(input, (err, input) => {
      if (err) {
        console.error(err);
        return;
      }
      // Set the Qr code once it is generated
      setQrCode(input);
    });
  };


  return (
    <div className="qr-generator">
      <div className="input-container">
        <input
          type="text"
          id="input"
          placeholder="Eg: https://google.com/ "
          value={input}
          onChange={handleInputChange}
        />
        <button className="submit" onClick={generateQRCode}>
          Generate
        </button>
      </div>
      {qrCode && (
      <div className="display-container">
        <img src={qrCode} alt="QR code" id="qr" />


        <a className="download-btn" href={qrCode} download="qrcode.png">Download QR Code</a>
      </div>
      )}
    </div>
  );
}


export default QrGenerator;

With adding the logic to the component, you will now be able to see the entire working of the controls and display. The output will look like this

Congratulations 🎉!

You have successfully created your own QR code generator application.

In the next lesson, we will look into the steps to deploy the project using vercel.

See you in the next lesson!