React
Implementation of the Qr grid library for React applications.
Installation
sh
npm install @qrgrid/reactsh
bun add @qrgrid/reactsh
pnpm install @qrgrid/reactsh
yarn add @qrgrid/reactUsing Canvas
tsx
import { Qr } from "@qrgrid/react/canvas";
<Qr input="Hello World!"/>OR
tsx
import { Canvas } from "@qrgrid/react";
<Canvas.Qr input="Hello World!"/>Props
| Prop | Type | Description | Default |
|---|---|---|---|
| input | string | Data to encode | |
| qrOptions? | QrOptions | Options for QR encoding | |
| image? | QrImageOption | Image options for the image in QR code | sizePercent: 15 opacity: 1 overlap: true border: false |
| size? | number | Canvas size | 400 |
| watchKey? | string | number | Key to watch for regenerating the QR code (besides input, size, qrOptions and image). | |
| bgColor? | QrColor | ((ctx: CanvasRenderingContext2D) => QrColor) | Background color | black |
| color? | QrColor | ((ctx: CanvasRenderingContext2D) => QrColor) | QR code color | white |
| moduleStyle? | ModuleStyleFunction | Custom styles for QR modules | |
| getQrData? | (qr: QR) => void; | Retrieve QR data | |
| getCanvasCtx? | (ctx: CanvasRenderingContext2D) => void; | Retrieve canvas context | |
| onGenerated? | (ctx: CanvasRenderingContext2D, size: number, qr: QR) => void; | Callback once QR code is generated |
Using Svg
tsx
import { Svg } from "@qrgrid/react";
<Svg.Qr input="Hello World!"/>OR
tsx
import { Qr } from "@qrgrid/react/svg";
<Qr input="Hello World!"/>Props
| Prop | Type | Description | Default |
|---|---|---|---|
| input | string | Data to encode | |
| qrOptions? | QrOptions | Qr Options for QR encoding | |
| image? | QrImageOption | Image options for image in the QR code | sizePercent: 15 opacity: 1 overlap: true border: false |
| size? | number | SVG size | 400 |
| watchKey? | string | number | Key to watch for regenerating the QR code (besides input, size, qrOptions and image). | |
| bgColor? | string | Background color | black |
| color? | string | { codeword?: string; finder?: string } | QR code color | white |
| moduleStyle? | ModuleStyleFunction | Custom styles for QR modules | |
| getQrData? | (qr: QR) => void; | get Qr data | |
| onGenerated? | (path: ModuleStyleFunctionParams[0], size: number, qr: QR) => void; | Callback once QR code is generated |
Examples
Explore Styles for custom styling and other utilities
Basic
tsx<Qr input="Hello World!" />ErrorCorrection
tsx<Qr input="Hello World!" qrOptions={{errorCorrection: 'H'}} />Color
tsx<Qr input="Hello World!" bgColor="#F8EDED" color="#173B45" />Module Style from @qrgrid/styles
tsximport { dotModuleStyle } from "@qrgrid/styles/canvas/styles"; <Qr input="Hello World!" moduleStyle={dotModuleStyle} />tsximport { smoothModuleStyle } from "@qrgrid/styles/canvas/styles"; <Qr input="Hello World!" moduleStyle={smoothModuleStyle} />Images
tsx<Qr input="Hello World!" image={{ src: "./vite.svg" }} />tsx<Qr input="Hello World!" image={{ src: "./vite.svg", overlap: false }} />Downloading using @qrgrid/styles
tsximport { Qr } from "@qrgrid/react/canvas"; import { downloadQr } from "@qrgrid/styles/canvas/utils"; const qrRef = useRef<HTMLCanvasElement | null>(null); const download = () => { if (qrRef.current) { downloadQr(qrRef.current); } }; <Qr input="Hello World!" ref={qrRef} />tsximport { Qr } from "@qrgrid/react/svg"; import { downloadQr} from "@qrgrid/styles/svg/utils"; const qrRef = useRef<SVGSVGElement | null>(null); const download = () => { if (qrRef.current) { downloadQr(qrRef.current); } }; <Qr input="Hello World!" ref={qrRef} />
For more examples and customization options, see the examples.