Create a Business Card QR Code in React

Banner for a MediaJam post

Milecia

In recent years, QR codes have become more popular to give out information that used to be printed. Restaurant menus, concert tickets, and even documentation for getting through customs in certain countries have been moved to QR codes. They let us share that info quickly and in a sustainable manner.

One thing that hasn't gone away is the business card. LinkedIn profiles are nice, but sometimes having that custom card means a little more depending on the environment. Going to online and in-person conferences could definitely benefit from having business cards available. That's why we're going to create a QR code we can let others scan to see our business card. No more ordering hundreds of these and packing them everywhere.

Some basic setup

The first thing we need to do is create the React app we'll work with. To do that, run the following command:

1$ npx create-react-app qr-card --template typescript

This will bootstrap a new React app for us. Next, you'll need to install the package we'll be using to generate the custom QR codes, react-qr-code. Run the following command:

1$ npm i react-qr-code

We'll also be using Material UI to style the components we'll work with so run the following command to install these dependencies:

1$ npm i @mui/material @emotion/react @emotion/styled

This is the only setup we have to do. Let's jump into the real code.

Make the business card input

Go to the src directory and add a new directory called components. Inside that folder, add a new file called QrGenerator.tsx. This is where we'll have the form to create a business card based on what users enter and we'll display the generated QR code.

Open this new file and add the following code:

1import { useState } from "react";
2import QRCode from "react-qr-code";
3import Button from "@mui/material/Button";
4import Container from "@mui/material/Container";
5import Stack from "@mui/material/Stack";
6import TextField from "@mui/material/TextField";
7
8export default function QrGenerator() {
9 const [qrValue, setQrValue] = useState("placeholding");
10
11 function generateCode(e: any) {
12 e.preventDefault();
13
14 setQrValue(`
15 <div>
16 <p>${e.currentTarget.fullName.value}</p>
17 <p>${e.currentTarget.jobTitle.value}</p>
18 <p>${e.currentTarget.shortDescription.value}</p>
19 <p>${e.currentTarget.website.value}</p>
20 </div>
21 `);
22 }
23
24 return (
25 <Container>
26 <form
27 onSubmit={(e) => generateCode(e)}
28 style={{ marginBottom: "24px", width: "800px" }}
29 >
30 <Stack spacing={2}>
31 <TextField
32 id="fullName"
33 label="Full Name"
34 variant="standard"
35 name="fullName"
36 />
37 <TextField
38 id="jobTitle"
39 label="Job Title"
40 variant="standard"
41 name="jobTitle"
42 />
43 <TextField
44 id="shortDescription"
45 label="Short Description"
46 variant="standard"
47 name="shortDescription"
48 />
49 <TextField id="email" label="Email" variant="standard" name="email" />
50 <TextField
51 id="website"
52 label="Website"
53 variant="standard"
54 name="website"
55 />
56 <Button variant="contained" type="submit">
57 Generate code
58 </Button>
59 </Stack>
60 </form>
61 <QRCode value={qrValue} style={{ display: "block", margin: "0 auto" }} />
62 </Container>
63 );
64}

Now let's discuss what is happening here. First, we import all of the methods and components we need. Next, we define the QrGenerator component. Inside of this component, we define a new state that will update the value users will see when they scan the QR code.

From here, we create the generateCode function that takes the input from our business card form and updates the qrValue state variable. Next, we dive into the return statement where we render all of the elements.

We start with the <form> where we call the generateCode function on submission. Then we use MUI to add the fields we need for the form. So we have inputs for a name, job title, short description, email address, and website URL with a submit button at the end. The reason these fields have been chosen is that you might want to showcase different experiences to people when you give them your QR code.

Finally, we display the code using the QRCode component from the package we imported. This takes the state that gets updated with each form submission and displays it to people that scan the code. That's all we have to do to create this custom QR code on demand! This is great for networking at conferences when you want to say something more memorable on your cards.

Generate the QR code

Now go to the App.tsx file and delete all of the boilerplate code. Then add the following code to show the business card form:

1import QrGenerator from "./components/QrGenerator";
2
3export default function App() {
4 return <QrGenerator />;
5}

This is how we display our custom component when the app starts up. You can add more formatting to the value that gets displayed or just redirect them to a URL. The main thing is that you can change the QR code value on the fly, giving you some nice flexibility in how you present your business cards to different people.

the form and QR code

Finished code

You can check out all of the code in the qr-card folder of this repo. You can also check out the app in this Code Sandbox.

Conclusion

Anything that you used to print off can probably be transformed into a QR code. One thing to keep in mind is that some things do need to be printed off to give the full effect. Sometimes opening a hand-written thank you card is better than receiving a custom animated thank you through a QR code. For many things though, people will likely appreciate not needing to carry more paper or touch more things.

Milecia

Software Team Lead

Milecia is a senior software engineer, international tech speaker, and mad scientist that works with hardware and software. She will try to make anything with JavaScript first.