React QRCode Generator Tutorial

React QRCode Generator Tutorial

In this tutorial, we will learn how to create a simple QR Code generator using the react-qr-code npm package. Before we begin, we'd need to first clone the repository with the initial boilerplate code. Then you are going to install the packages.

git clone https://github.com/rbarbosa51/react-qrcode-generator-tutorial.git

cd react-qrcode-generator-tutorial
npm install        

Once you finish the installation, open it with your favorite code editor. Take a minute to review the code. It is a simple TypeScript-React project, made with Vite. It has been preconfigured with Tailwind CSS for styling. run the development server and you should see the following

Article content

Install the "react-qr-code" package

npm install react-qr-code        

Go to the "App.tsx" file, you are going to import the library and the useState hook.

import QRCode from "react-qr-code"
import { useState } from "react"        

We are now going to add a state. The state will hold the value of the input element. This input element will be passed to the <QRCode /> component. Write the following:

...
function App() {
  const [qrText, setQrText] = useState<string | null>(null);

...        

Now you are going to make some modifications to the code. You are going to wrap the Nothing box as seen below.

...
{qrText ? (
  <QRCode value={qrText} />
) : (
  // This was the original code
  <div className="flex aspect-square w-64 items-center justify-center border-2 border-solid border-purple-500">
   <div>Nothing</div>
  </div>
)}
...        

Now we are going to create a function to handle input changes to the input element. This function will change the state, based on the input.

const handleChange = (e: React.BaseSyntheticEvent) => {
  setQrText(e.target.value);
};        

Then modify the input tag to add an onChange attribute that takes the handleChange function we just created. It should look like the following:

<input
   className="rounded-md px-2 outline outline-1 focus:outline-none"
   type="text"
   name="qrValue"
   id="qrValue"
   onChange={handleChange}
 />        

Now run the development server, and type anything into the input. You should now see the QR code

Article content

The finished code should look like:

import QRCode from "react-qr-code";
import { useState } from "react";

function App() {
  const [qrText, setQrText] = useState<string | null>(null);
  const handleChange = (e: React.BaseSyntheticEvent) => {
    setQrText(e.target.value);
  };
  return (
    <div className="min-h-screen bg-gradient-to-b from-lime-100 to-purple-300 text-center text-purple-500">
      <h1 className="text-6xl">QR Code</h1>
      <div className="mt-16 flex flex-col items-center justify-center gap-4">
      {qrText ? (
          <QRCode value={qrText} />
        ) : (
          // This was the original code
          <div className="flex aspect-square w-64 items-center justify-center border-2 border-solid border-purple-500">
            <div className="">Nothing</div>
          </div>
        )}
        
        <label htmlFor="qrValue">Write a text to convert it to QR code</label>
        <input
          className="rounded-md px-2 outline outline-1 focus:outline-none"
          type="text"
          name="qrValue"
          id="qrValue"
          onChange={handleChange}
        />
      </div>
    </div>
  )
}
export default App        

If you get stuck, feel free to clone the finished code

git clone https://github.com/rbarbosa51/react-qrcode-generator-tutorial.git -b Finished        

I sincerely hope that you enjoyed this tutorial. I want to hear from you.

Are you looking for a web developer to join your team? I am looking for a new team to belong to; so feel free to contact me.

Please view my Portfolio:

https://rafael-barbosa.com


To view or add a comment, sign in

More articles by Rafael Barbosa

  • React 19 Compiler Tutorial

    In this quick tutorial, I will show you how you can upgrade a React-Vite program to use the new React 19 Compiler. For…

  • Learn how to use Zustand: the best Redux alternative.

    Zustand is a very simplistic alternative to the Redux state management library. It is fast intuitive and overall simple…

    1 Comment
  • Format.JS (React-Intl) Tutorial

    In this tutorial, we are going to learn how to localize your React app text. This can be very handy whenever we want to…

  • React Sentiment Analysis Tutorial

    In this tutorial, We will use a Hugging Face (https://huggingface.co/) AI-pretrained model to create a React sentiment…

    1 Comment
  • React Three Fiber's Pointer Events Tutorial

    In this React Three Fiber tutorial, we will learn how to create simple animations based on Pointer events. First, we…

    8 Comments
  • React Error Boundaries Components with TypeScript

    In this quick tutorial, we are going to learn how to use Error Boundaries in React with TypeScript. By the end of this…

  • React Three Fiber Tutorial

    In this simple tutorial, we are going to learn how to manipulate a 3D model with the use of the React-Three-Fiber…

    1 Comment

Others also viewed

Explore content categories