Developers

QR codes in React without a library

You can show a QR code in React with a plain img element whose src is https://qrcodesforever.com/qr.svg plus your content, built with URLSearchParams. Nothing gets installed and your bundle stays the same size. The code is drawn on our server, cached for a year, and loaded like any other image.

<QrCode value="https://example.com/menu" size={160} />

That renders a 160-pixel SVG QR code for the menu link. Here's the component behind it.

The component

type QrCodeProps = {
  value: string
  size?: number
  fg?: string // hex without #, e.g. "1d3fd6"
  label?: string
}

export function QrCode({ value, size = 200, fg, label }: QrCodeProps) {
  if (!value.trim()) return null
  const params = new URLSearchParams({ url: value })
  if (fg) params.set("fg", fg)
  return (
    <img
      src={`https://qrcodesforever.com/qr.svg?${params}`}
      width={size}
      height={size}
      alt={label ?? `QR code for ${value}`}
    />
  )
}

URLSearchParams encodes the value, so a link like https://example.com/?a=1&b=2 keeps its &b=2. Built by hand, the code would quietly hold https://example.com/?a=1. The early return null matters because an empty value gets a 400 from the API, which shows as a broken image.

The width and height aren't optional. The SVG has a viewBox but no size of its own, so without them the browser picks a size, rarely the one you wanted, and the layout jumps when the image arrives.

Other kinds of code need other fields. For a Wi-Fi code, swap the params for { type: "wifi", ssid: "Cafe Guest", password: "coffee123" }; the code then contains WIFI:T:WPA;S:Cafe Guest;P:coffee123;;. The embed reference lists every kind.

Live preview while someone types

Each new value is a new image request. Wait until typing pauses:

import { useEffect, useState } from "react"

function useDebounced<T>(value: T, ms = 300) {
  const [v, setV] = useState(value)
  useEffect(() => {
    const t = setTimeout(() => setV(value), ms)
    return () => clearTimeout(t)
  }, [value, ms])
  return v
}

// const debounced = useDebounced(text)
// <QrCode value={debounced} />

A half-typed link like https://exam fails validation and returns a 400, so for URL inputs, check the value looks complete before passing it on.

A download button that downloads

Browsers ignore the download attribute on links to another site, so <a href={pngUrl} download> just opens the image. Fetch it instead. The API sends Access-Control-Allow-Origin: *, so this works from any origin:

async function downloadPng(value: string) {
  const q = new URLSearchParams({ url: value, size: "1024" })
  const res = await fetch(`https://qrcodesforever.com/qr.png?${q}`)
  if (!res.ok) throw new Error(await res.text())
  const href = URL.createObjectURL(await res.blob())
  const a = document.createElement("a")
  a.href = href
  a.download = "qr-code.png"
  a.click()
  setTimeout(() => URL.revokeObjectURL(href), 1000)
}

When to install a library instead

An image URL needs a network connection, and it sends the value to our server. A point-of-sale screen that must work offline, or an app encoding something private, should use a client-side package such as qrcode and draw on a canvas. For a marketing site or dashboard that's online anyway, the image keeps the component to a dozen lines. Using next/image? Read the Next.js guide first, because SVG URLs need the unoptimized prop. Plain HTML pages are covered in QR codes in HTML.

Written by Last updated