Skip to content

React

Implementation of the Qr grid library for React applications.

Installation

sh
npm install @qrgrid/react
sh
bun add @qrgrid/react
sh
pnpm install @qrgrid/react
sh
yarn add @qrgrid/react

Using 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

PropTypeDescriptionDefault
inputstringData to encode
qrOptions?QrOptionsOptions for QR encoding
image?QrImageOptionImage options for the image in QR codesizePercent: 15 opacity: 1 overlap: true border: false
size?numberCanvas size400
watchKey?string | numberKey to watch for regenerating the QR code (besides input, size, qrOptions and image).
bgColor?QrColor | ((ctx: CanvasRenderingContext2D) => QrColor)Background colorblack
color?QrColor | ((ctx: CanvasRenderingContext2D) => QrColor)QR code colorwhite
moduleStyle?ModuleStyleFunctionCustom 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

see the props in code.

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

PropTypeDescriptionDefault
inputstringData to encode
qrOptions?QrOptionsQr Options for QR encoding
image?QrImageOptionImage options for image in the QR codesizePercent: 15 opacity: 1 overlap: true border: false
size?numberSVG size400
watchKey?string | numberKey to watch for regenerating the QR code (besides input, size, qrOptions and image).
bgColor?stringBackground colorblack
color?string | { codeword?: string; finder?: string }QR code colorwhite
moduleStyle?ModuleStyleFunctionCustom 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

see the props in code.

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

    tsx
    import { dotModuleStyle } from "@qrgrid/styles/canvas/styles";
    
    <Qr input="Hello World!" moduleStyle={dotModuleStyle} />
    tsx
    import { 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

    tsx
    import { 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} />
    tsx
    import { 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.

Released under the MIT License.