Create QR Code using Reactjs (TypeScript + Download PNG)
Build a complete QR code generator in React with live preview, color customization, size controls, and one-click PNG download.
In this tutorial, you will build a full QR Code Generator using React and TypeScript. By the end, users can type any text or URL, customize size and colors, preview changes instantly, and download the QR as a PNG image.
What You Will Build
- Live QR generation while typing
- Custom foreground and background colors
- Adjustable QR size
- Download QR as PNG
- Type-safe React component architecture
Step 1: Create a React + TypeScript Project
Use Create React App with the TypeScript template to bootstrap the project quickly.
Step 2: Install Required Modules
Install the QR package plus a file download helper.
- react-qr-code: generates QR SVG from text or URL
- file-saver: useful helper for downloads in browser apps
Step 3: Project Folder Structure
Keeping the QR UI in a separate component makes the code easier to maintain and extend.
Step 4: Create the QRGenerator Component
Create src/QRGenerator.tsx and paste the following code.
Step 5: Update App.tsx
Now render the QR generator in your app root component.
Step 6: Run the Project
When the app opens in your browser, you will get live QR updates, custom colors, dynamic sizing, and PNG export from the same screen.
How It Works
useState
React state stores the QR text, size, and colors. Each time state changes, the component re-renders and the QR image updates in real time.
useRef
A ref gives direct access to the rendered QR SVG element. That makes it possible to serialize the SVG and convert it to a downloadable PNG.
XMLSerializer + Canvas
The browser serializes the SVG into a string, then loads it into an image object. Canvas draws that image and exports PNG data with toDataURL, which is then downloaded through a temporary anchor element.
Optional Improvements
- Validate input and show an error for empty text
- Add presets for social links, WiFi, and contact cards
- Export as SVG in addition to PNG
- Persist the latest settings in localStorage
- Add a copy-to-clipboard button for the source text
Ready to create your free QR code?
No sign-up required. Generate, customise, and download in seconds.
Create QR Code FreeMore Articles
View allStatic vs Dynamic QR Codes: Which Should You Use?
Static QR codes are free and never expire. Dynamic codes are trackable and editable. Here's exactly …
ReadUnlocking Convenience: How to Use a QR Code Generator
Discover the power of QR code generators and learn how to create, customize, and deploy QR codes eff…
Read