DEV Community

Cover image for Add QR code to React websites in 2 minutes 😎✨
Tapajyoti Bose
Tapajyoti Bose

Posted on • Updated on

Add QR code to React websites in 2 minutes 😎✨

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

Dependencies

There are a lot of libraries that can be used to generate the QR codes. My personal favorite is qrcode, which I would be using in this article.

Install the package using:

npm i qrcode
Enter fullscreen mode Exit fullscreen mode

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.

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>
  );
}
Enter fullscreen mode Exit fullscreen mode

Generating the QR code

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

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>
  );
}
Enter fullscreen mode Exit fullscreen mode

The following is the end result

qr-code

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.

<!-- index.html -->
<div>
  <input id="text-input" />
  <br />
  <canvas id="qr-code-canvas"></canvas>
</div>
Enter fullscreen mode Exit fullscreen mode
// 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)
  );
});
Enter fullscreen mode Exit fullscreen mode

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

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

Thanks for reading

Need a Top Rated Front-End Development Freelancer to chop away your development woes? Contact me on Upwork

Want to see what I am working on? Check out my Personal Website and GitHub

Want to connect? Reach out to me on LinkedIn

I am a Digital Nomad and occasionally travel. Follow me on Instagram to check out what I am up to.

Follow my blogs for Weekly new Tidbits on Dev

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
    2. Front End Project Ideas
  2. Would you mentor me?

    Sorry, I am already under a lot of workload and would not have the time to mentor anyone.

Top comments (2)

 
ruppysuppy profile image
Tapajyoti Bose

GH is GitHub, wow that's breaking news to me 🤯

I would love to know where is it given the library is "dangerous"

Collapse
 
ruppysuppy profile image
Tapajyoti Bose

As-given on GitHub?