How Pros add QR code to their sites 😎✨

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.

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!

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

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>
);
}

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>
);
}

The following is the end result

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>
// 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!

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! 🎉

Research says, writing down your goals on pen & paper makes you 21% to 39% more likely to achieve them. Check out these notebooks and journals to make the journey of achieving your dreams easier: https://www.amazon.com/Tapajyoti-Bose/e/B09VGDDHRR

Follow me for weekly new tidbits on the domain of tech!

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tapajyoti Bose

Top Rated Freelancer || Blogger || Cross-Platform App Developer || Web Developer || Open Source Contributor || FIRE Enthusiast