React library

Get started with the React library

Install qrdx, render a styled QR code in a React component and save it as a file, in a few minutes.

By the end of this page you will have a styled QR code rendered by a React component and a button that saves it as an SVG. The library draws the code itself, so you need no account and no API key.

Before you start

  • A React app on React 16.8 or newer. Next.js, Vite, Remix and the others all work, and so do apps on webpack 4, such as Create React App 4.
  • Node.js 18 or newer.
  • TypeScript 5.0 or newer, if your app uses TypeScript.

Want to see the options before you write any code? Style a code in the playground and copy it as a React component.

Build it

Install qrdx

npm install qrdx

react is a peer dependency, so the package uses the version your app already has. Nothing else is installed: PDF export needs the optional jspdf package, and only when you use it.

Render a code

components/first-qr-code.tsx
import { QRCode } from "qrdx";

export function FirstQrCode() {
  return <QRCode value="https://example.com" size={256} />;
}

Put the component on a page. You see a 256 px code in the library's default look: round modules, gear-shaped corner eyes and black on white. Point a phone camera at the screen and it opens example.com.

The code components/first-qr-code.tsx draws: a black QR code on white in the library's default look, with round modules and gear-shaped corner eyes.

QRCode renders an <svg> and uses no browser-only hooks, so it works in React Server Components and renders in the page's HTML before any JavaScript runs. Screen readers get nothing from it on its own: step 3 shows how to label it.

Style it

components/styled-qr-code.tsx
import { QRCode } from "qrdx";

export function StyledQrCode() {
  return (
    <QRCode
      value="https://example.com/menu"
      size={256}
      margin={4}
      fgColor="#1d4ed8"
      bodyPattern="rounded"
      cornerEyePattern="extra-rounded"
      cornerEyeDotPattern="rounded"
      frame={{ style: "banner-bottom", text: "Scan for the menu" }}
    />
  );
}

The code is now blue with rounded modules, and a banner under it reads "Scan for the menu". The output is taller than 256 px, because a frame is added around the code rather than drawn over it, so the code still scans.

The code components/styled-qr-code.tsx draws: blue rounded modules, extra-rounded corner eyes, and a banner under the code that reads Scan for the menu.

What each prop does:

  • margin is the quiet zone around the code, in modules. The default is 0, which suits a code inside a card with its own padding. On a busy or dark background, set at least 2 (the QR standard asks for 4): a code with no light border there may not scan.
  • fgColor takes any CSS color or a gradient. bgColor, eyeColor and dotColor work the same way.
  • bodyPattern, cornerEyePattern and cornerEyeDotPattern shape the modules, the corner eyes and the dots inside them. There are 25 body patterns, 22 eye shapes and 22 eye centers, all drawn live in Shapes, eyes and frames.
  • frame adds one of 25 built-in call-to-action frames, or a frame of your own.
  • Any other prop goes on the <svg>, so role="img" with aria-label="QR code for the menu" gives the code an accessible name, and className gives it a class. This needs qrdx 0.0.56 or later; older versions drop these props, so there you wrap the code in an element that carries them.

Save it as a file

components/download-svg-button.tsx
"use client";

import { downloadQRCode } from "qrdx";

const menuCode = {
  value: "https://example.com/menu",
  margin: 4,
  fgColor: "#1d4ed8",
  bodyPattern: "rounded",
  frame: { style: "banner-bottom", text: "Scan for the menu" },
} as const;

export function DownloadSvgButton() {
  return (
    <button
      type="button"
      onClick={() =>
        downloadQRCode(menuCode, {
          format: "svg",
          size: { width: 1024, height: 1024 },
          filename: "menu.svg",
        })
      }
    >
      Download SVG
    </button>
  );
}

Clicking the button saves menu.svg: the same code as on screen, drawn at 1024 px and sharp at any size, which is the file a print shop asks for. Set format to png, jpg, webp or bmp for an image, ico for a favicon, eps for a vector file for design tools, or pdf (needs the optional jspdf package).

downloadQRCode uses the browser's DOM, so the button lives in a Client Component. A Server Component can still render it. Keep the code's options in one object, as above, so the file matches the preview.

Check that it scans

Scan the code with two or three phones before you print it. For a design with unusual colors or a logo, getScanScore runs the same checks in the browser and returns a score, a grade and what to fix:

lib/check-code.ts
import { getScanScore } from "qrdx";

const { score, grade, checks } = await getScanScore({
  value: "https://example.com/menu",
  fgColor: "#1d4ed8",
  bodyPattern: "rounded",
});
// score: 0 to 100, grade: "great" | "ok" | "risky" | "fails"
// checks: [{ id, passed, message, fix? }]

You can also upload a screenshot or a photo of a printed code to the scan checker.

If something goes wrong

  • Module not found: Can't resolve 'qrdx'. Install the package in the workspace or app that imports it, then restart the dev server.
  • The code renders but phones don't read it. Add margin={4}, keep dark modules on a light background, and render it at least 128 px wide on screen. A code won't scan lists the other causes.
  • PDF export needs the optional "jspdf" package. Run npm install jspdf in the same package. SVG, PNG, JPG, WebP, GIF, BMP, ICO, TIFF and EPS need nothing extra.
  • Module parse failed with webpack 4, or Cannot find name 'NoInfer' from TypeScript. Update qrdx to 0.0.66 or newer: it ships a build for webpack 4 and types for TypeScript 5.0 to 5.3. TypeScript 4.9 and older can't read the types of zod, which qrdx uses.

Was this page helpful?

Last updated on

On this page