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!
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.
id="text-input" />
/>
// 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
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.
-
I am a beginner, how should I learn Front-End Web Dev?
Look into the following articles: -
Would you mentor me?
Sorry, I am already under a lot of workload and would not have the time to mentor anyone.