DEV Community

starpebble
starpebble

Posted on

4 1

Generating a QR code with React hooks and JavaScript

A single react function component can place an image like a QR code in a HTML5 canvas tag.

Screenshot

Alt Text

QRCode function component example

Here's how to do this without Kubernetes. This example is just JavaScript.

Example JavaScript:

const QRCode = ({text, size}) => {
  const canvas = useRef(null);
  useEffect(() => {
    if (canvas != null && canvas.current != null) {
      let qr = new QRious({
        element: canvas.current,
          value: text,
          size: size
      });
    }
  });
  return(<canvas ref={canvas}></canvas>);
}

Props

QRCode is a function component. QRCode accepts two props.

  1. text
  2. size

The generated QR code image encodes the text. Size controls the image size.

React hooks in this function component

The function component uses exactly two hooks:

  • useRef
  • useEffect

useRef() is an essential react hook. It's almost impossible for a React function component to draw on an HTML5 <canvas> without this hook.

useEffect() helps the function component by listening to React component lifecycle events.

Dependencies

Include the cdn hosted versions as <script/> tags:

unpkg is reliable. I love it. cdnjs is unstoppable! I love it too. Thanks Cloudflare!

Example

A single html file example.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>QRCode with React Hooks and JS</title>
    <meta charset="utf-8" />
    <meta name="description" content="QRCode with React Hooks and JS.">
    <meta name="keywords" content="JavaScript, React">    
    <meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
    <script src="https://unpkg.com/react@latest/umd/react.development.js" crossorigin="anonymous"></script>
    <script src="https://unpkg.com/react-dom@latest/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/babel-standalone@latest/babel.min.js" crossorigin="anonymous"></script>
    <script src="https://unpkg.com/qrious@latest/dist/qrious.min.js"></script> 
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">

// React Hooks
const {
  useRef,
  useEffect
} = React;

// QRCode
//
// Function component, creates one QR code image    
const QRCode = ({text, size}) => {
  const canvas = useRef(null);
  useEffect(() => {
    if (canvas != null && canvas.current != null) {
      let qr = new QRious({
        element: canvas.current,
          value: text,
          size: size
      });
    }
  });
  return(<canvas ref={canvas}></canvas>);
}

// App
//
const App = () => {
  const text = "https://dev.to";
  return(<div align="center">
  <h1>One QRCode</h1>
  <QRCode text={text} size="100"/>
  <p>{text}</p>     
  </div>);
}

ReactDOM.render(
  <App />,
  document.querySelector('#root'),
);
    </script>
  </body>
</html>

Just one way

There are dozens of ways to generate a single QR code image. I'm writing down a list. Please feel free to share the best libraries as a comment.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay