Add QR code to React websites in 2 minutes 😎✨
Tags: javascript, webdev, programming, react
Published: 2022-09-18 04:22:51

Love them or hate them, the cool kids around the block use them. If you want to develop an app that relies on sharing stuff, **QR codes** are a great tool to get users hooked.

> But it feels too difficult to implement

Fret not my friend, this article will guide you through such an easy solution, that you can do it, with your eyes closed (metaphorically speaking, of course).

Let's get started!

![lets-get-started](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f34fmoyrslyt35wsz9qc.gif)

## Dependencies

There are a lot of libraries that can be used to generate the **QR codes**. My personal favorite is [qrcode](https://www.npmjs.com/package/qrcode), which I would be using in this article.

Install the package using:

```bash
npm i qrcode
```

## Setting up the component

We would start off with a basic component with a **text input field** as we need some input data to generate the **QR code**.

```jsx
import { useState } from "react";

export default function App() {
  const [text, setText] = useState("");

  return (
    <div>
      <input
        value={text}
        onChange={(e) => setText(e.target.value)}
      />
      <br />
    </div>
  );
}
```

## Generating the QR code

To generate the **QR code**, we would need a `canvas` element to render it out.

```jsx
import QRCode from "qrcode";
import { useEffect, useRef, useState } from "react";

export default function App() {
  const [text, setText] = useState("");
  const canvasRef = useRef();

  useEffect(() => {
    QRCode.toCanvas(
      canvasRef.current,
      // QR code doesn't work with an empty string
      // so we are using a blank space as a fallback
      text || " ",
      (error) => error && console.error(error)
    );
  }, [text]);

  return (
    <div>
      <input
        value={text}
        onChange={(e) => setText(e.target.value)}
      />
      <br />

      <canvas ref={canvasRef} />
    </div>
  );
}
```

The following is the end result

![qr-code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tn1hfzj21vurru9ec7s.gif)

## Using QR code in vanilla JS

From the package, it would have been evident, that it is NOT dependent on **React** to get the job done. **You can use it with any other framework or even vanilla JS**.

```html
<!-- index.html -->
<div>
  <input id="text-input" />
  <br />
  <canvas id="qr-code-canvas"></canvas>
</div>
```

```js
// script.js
const QRCode = require("qrcode");

const input = document.getElementById("text-input");
const canvas = document.getElementById("qr-code-canvas");

input.addEventListener("change", (event) => {
  QRCode.toCanvas(
    canvas,
    event.target.value || " ",
    (error) => error && console.error(error)
  );
});
```

## Use cases

Although the **QR codes** can be used to store any data, the most frequent use cases are:

### 1. Sharing links & contact details

**QR code offers unparalleled ease of sharing links** and **contact details**. All you need is to scan the code with any app (**Google Lens** is my personal favorite) and you are good to go!

![qr-code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxirxn86n36g6chaxot4.png)

### 2. Virtual Cards & tickets

**QR codes** can be used for the purpose of verification too. Just slap them onto **virtual cards** or **tickets**, and you can just scan them to verify the permissions or authenticity.

### 3. Adding a digital touch

Want to inform your users of all the features of your product, but find it hard to cram into a single advertisement? Add a **QR code** with a link to your product details page and let the users explore it on their own.

Hope this article made it clear how simple it is to add **QR codes** to your website!

**That's all folks! 🎉**

Finding **personal finance** too intimidating? Checkout my **Instagram** to become a [**Dollar Ninja**](https://www.instagram.com/the.dollar.ninja/)

# Thanks for reading

Need a **Top Rated Front-End Development Freelancer** to chop away your development woes? Contact me on [Upwork](https://www.upwork.com/o/profiles/users/~01c12e516ee1d35044/)

Want to see what I am working on? Check out my [Personal Website](https://tapajyoti-bose.vercel.app) and [GitHub](https://github.com/ruppysuppy)

Want to connect? Reach out to me on [LinkedIn](https://www.linkedin.com/in/tapajyoti-bose/)

I am a Digital Nomad and occasionally travel. Follow me on [Instagram](https://www.instagram.com/tapajyotib/) to check out what I am up to.

Follow my blogs for **Weekly new Tidbits** on [Dev](https://dev.to/ruppysuppy)

**FAQ**

These are a few commonly asked questions I get. So, I hope this **FAQ** section solves your issues.

1. **I am a beginner, how should I learn Front-End Web Dev?**
   Look into the following articles:

    1. [Front End Development Roadmap](https://dev.to/ruppysuppy/front-end-developer-roadmap-zero-to-hero-4pkf)
    2. [Front End Project Ideas](https://dev.to/ruppysuppy/5-projects-to-master-front-end-development-57p)

2. **Would you mentor me?**
   Sorry, I am already under a lot of workload and would not have the time to mentor anyone.