NextJs Example
This is a simple example of nextjs with react and tailwindcss. you can use this as a starting point for your own project.
First you need to setup the NextJs project. You can refer to NextJs documentation for that: https://nextjs.org/docs (opens in a new tab)
This package supports all the latest version of NextJs.
After setting up, install the upiqr package using npm:
npm i @omkarbhosale/upiqr
After installation, you can now make a custome component for generating UPI QR code in your NextJs project.
// app/components/Qrcode.jsx
"use client";
import { useState, useEffect } from "react";
import Image from "next/image";
import generateQR from "@omkarbhosale/upiqr";
const Qrcode = ({ amount }) => {
const [qrCode, setQrCode] = useState(null);
const [error, setError] = useState(null);
console.log(process.env.NEXT_PUBLIC_UPI_ID); // Save your UPI ID in .env file and check on the client console if it is working!
useEffect(() => {
const fetchQRCode = async () => {
try {
// Generate the QR code as a Base64 URL
const data = await generateQR({
UPI_ID: process.env.NEXT_PUBLIC_UPI_ID,
AMOUNT: amount,
});
setQrCode(data);
} catch (error) {
setError("Error generating QR code");
console.error("Error generating QR code:", error);
}
};
fetchQRCode();
}, [amount]); // QR Code will be automatically updated when amount changes.
return (
<div>
<h2>UPI QR Code</h2>
{error ? (
<p>{error}</p>
) : qrCode ? (
<Image src={qrCode} width={200} height={200} alt="QR Code image" />
) : (
<p>Generating QR Code...</p>
)}
</div>
);
};
export default Qrcode;
Following is a full breakdown of your Qrcode
component and how each part works:
-
Imports:
useState
anduseEffect
are React hooks used for managing state and side effects.Image
component from the Next.js Image library is used to display the generated QR code image.generateQR
is imported from your@omkarbhosale/upiqr
package and is responsible for generating a QR code as a Base64-encoded URL.
-
Component Definition:
Qrcode
is a functional component that takes two props,upi_id
andamount
, representing the UPI ID and the amount to include in the QR code.
-
State Variables:
qrCode
: Stores the Base64 URL of the generated QR code image. Initially, it’snull
.error
: Stores any error message if QR code generation fails. Initially, it’snull
.
-
useEffect:
useEffect
is used here to perform a side effect — fetching a QR code when the component mounts or whenever theupi_id
oramount
props change.- Dependency Array
[upi_id, amount]
: Addingupi_id
andamount
as dependencies ensures thatfetchQRCode
re-runs each time these props change, so if a new UPI ID or amount is passed, a new QR code is generated.
-
fetchQRCode Function:
- Defined as an
async
function to allow the use ofawai
t for generating the QR code. - Try-Catch Block:
- Inside the
try
block,generateQR
is called with theupi_id
andamount
props, and the result is saved in thedata
variable. - The resulting
data
(Base64 URL for the QR code image) is then stored inqrCode
usingsetQrCode(data)
. - If an error occurs,
setError
is called to store a custom error message, and the error details are logged to the console.
- Inside the
- Defined as an