How to generate and download a QR code image in react?

Imran Ahmad
1 min readMay 11, 2019

--

👋 Hi there, let connect on Twitter

Coming straight to the point.

Generating the QR code

1: Install the qrcode.react npm package.

npm install qrcode.react -s

2: Render QR image

import QRCode from "qrcode.react";// other code ...const downloadQR = () => { // will implement soon}<div>
<QRCode
id="123456"
value="123456"
size={290}
level={"H"}
includeMargin={true}
/>
<a onClick={downloadQR}> Download QR </a>
</div>

The QR will be rendered with Download QR link.

Downloading the QR image

1: Create a link without text so that it won’t be visible to users.

2: Assign the image URL and name.

3: Append the link as a child element to the body.

4: Click the created link by javascript to download the image.

5: Remove the created and invisible link.

const downloadQR = () => {
const canvas = document.getElementById("123456");
const pngUrl = canvas
.toDataURL("image/png")
.replace("image/png", "image/octet-stream");
let downloadLink = document.createElement("a");
downloadLink.href = pngUrl;
downloadLink.download = "123456.png";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
};

Thanks for the quick read.
Show your love by clapping. Ah! you can clap 50 times, if you are not aware.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Imran Ahmad
Imran Ahmad

Written by Imran Ahmad

Tech enthusiast, product visualiser nothing more or less.

Responses (10)

Write a response