Site icon Filestack Blog

Integrating Filestack QR Codes into Your React Web Application

Integrating Filestack QR Codes into Your React Web Application

What do you think of how QR codes are revolutionizing technology? Approximately 11 million US households regularly scan QR codes. Such codes have become indispensable in numerous industries. These include retail, marketing, healthcare, and education. By simply scanning, one retrieves information with little difficulty.

Developers can use QR code leads to keep users engaged with their applications. However, this task becomes challenging without the necessary software tools. Filestack simplifies the generation of QR codes and integrates the process into a React App.

This blog addresses QR code implementation in web applications. We will guide you through these tasks. First, we explain what QR codes are and their relevance. Then, we introduce the procedure for creating and integrating them using Filestack’s API. Let’s get started. 

What is a QR Code?

A QR (Quick Response) code is a type of barcode that holds more information than a regular one. It stores things like website links, product details, or event tickets. You can access this by scanning the code with your phone or a scanner. For example, scanning a code in a menu might take you to an order page, or in stores, it shows more product details.

QR codes are used in many areas. In stores, they provide product information. In ads, they link to promotions or loyalty programs. Hospitals use them for managing patient records. They also make payments through apps like Venmo and PayPal easier. QR codes help with event check-ins, transport tickets, and even sharing school materials.

What are the real-life applications that help us create QR codes?

Here are some popular apps that help create QR codes:

Filestack

Filestack enables QR code generation within web applications through an API.

QR Code Generator

QR code generator allows users to create codes for several file types. It is available for free online or in mobile apps.

Google Chrome

Google Chrome lets smartphone users create QR codes from the share menu without leaving the site.

Canva

Canva offers a QR code generator among its tools. Users can generate QR codes in stylized formats.

QR Code Monkey

QR Code Monkey provides advanced features like high-quality codes with logos and colors.

Beaconstac

Beaconstac offers a QR Code generator for businesses. It includes dynamic codes, analytics, and customization options.

What is the best method to generate QR codes within your web application?

Filestack’s QR code generator simplifies the process. It encodes URLs quickly for web applications. You provide either a Filestack handle or an external URL. The application checks the resource before encoding it. It does not alter the file data.

Several customization options are available:

  1. Version: This controls the number of black-and-white modules. The versions range from 1 to 40. Version 1 has 21×21 modules. Each higher version adds four more modules per side.
  2. Error Correction: This feature helps recover data if the QR code is damaged. You can choose L (7% recovery), M (15%), Q (25%), or H (30%).
  3. Format: You can output the QR code as PNG or SVG, depending on your needs.

For example, to create a QR code using a Filestack handle:

https://cdn.filestackcontent.com/qr/HANDLE

You can add parameters:

https://cdn.filestackcontent.com/qr=version:4,error_correction:L,format:svg/HANDLE

How do you integrate Filestack’s QR codes feature?

Here are the steps to integrate Filestack’s QR codes feature:

Prerequisites

The first step towards integration is to get an API key. Create a new account at Filestack and navigate to the dashboard. The API key will be in the top right corner. The next step is to ensure that the API key is working correctly. 

Testing the API requests

Create an account at Postman and navigate to the main interface. Here, you will see an option to create a new request. You should make a new GET request. In that new request bar, you should add the below-given URL along with your API key as under:

Add your API key and the link to the image you want to convert to a QR code. Then, click on the Send button. You will see the response as shown below:

It means that our API key is working fine. Let’s build our sample application now. 

Building a sample application

Before building an application, we must ensure React JS is installed on our system. You can download it directly from React’s official website. After installing React, follow the steps given below to build your application. 

1. Set up Your React Environment

Step 1: Install Node.js and npm

Before we start, ensure you have Node.js installed on your machine, as it includes npm, which we’ll use to create the React app.

You can check if Node.js is installed by running:

node -v

Check if npm is installed:

npm -v

Step 2: Create a New React App

Now, let’s create a new React application using the command npx (comes with npm):

npx create-react-app filestack-qr-generator

Navigate to the project directory:

cd filestack-qr-generator

2. Install the Filestack SDK

We’ll need the filestack-js library to handle file uploads. You can install it using npm:

npm install filestack-js

3. Modify the Project Structure

Let’s modify the project by creating the necessary components and styles.

Step 1: Update App.js

Replace the contents of the default App.js with the code for the QR code generator functionality:

4. Step-by-Step Breakdown of App.js

1. Importing Required Libraries

At the top of the App.js file, we import the necessary libraries:

import React, { useState } from "react";

import { init } from "filestack-js";

import "./App.css";

2. Initializing the Filestack Client

Next, we initialize the Filestack client with an API key:

const client = init("Add-Your-API-Key-Here");

3. Creating the Functional Component

The main component of the app is the App functional component. Inside it, we use the useState hook to manage the state of the QR code URL:

function App() {

  const [qrCodeUrl, setQrCodeUrl] = useState(null);

4. Handling File Uploads

The handleUpload function is responsible for opening the Filestack file picker and processing the uploaded file:

const handleUpload = () => {

  client

    .picker({

      accept: "image/*",

      onUploadDone: (res) => {

        const handle = res.filesUploaded[0].handle;

        generateQRCode(handle);

      },

    })

    .open();

};

5. Generating the QR Code

The generateQRCode function takes the file handle and generates the QR code URL:

const generateQRCode = (handle) => {

  const qrUrl = `https://cdn.filestackcontent.com/apikey/qr/${handle}`;

  setQrCodeUrl(qrUrl);

};

6. Rendering the UI

In the return block of the component, we define the UI structure:

return (

  <div className="container">

    <img

      src="https://img.freepik.com/premium-vector/qr-code-cartoon-character-searching-with-magnifying-glass-cute-design_152558-13614.jpg"

      alt="Cartoon Banner"

      className="banner-img"

    />

    <h1>Filestack QR Code Generator</h1>

    <button id="uploadBtn" onClick={handleUpload}>

      Upload File

    </button>




    <div id="qrCode">

      {qrCodeUrl && (

        <img src={qrCodeUrl} alt="QR Code" style={{ maxWidth: "100%", height: "auto" }} />

      )}

    </div>




    {qrCodeUrl && (

      <a id="downloadLink" href={qrCodeUrl} download="qrcode.png">

        <button id="downloadBtn">Download QR Code</button>

      </a>

    )}

  </div>

);

Here’s how each part works:

7. Exporting the Component

Finally, the App component is exported to be used in other parts of the application:

export default App;

Complete App Flow

  1. File Upload

The user clicks the “Upload File” button, which triggers the Filestack file picker.

  1. File Handling

Once a file is uploaded, the file’s handle is retrieved from Filestack.

  1. QR Code Generation

A QR code is generated based on the file handle, and the QR code image is displayed in the browser.

  1. Download Option

The user can download the generated QR code by clicking the “Download QR Code” button.

What’s Happening Under the Hood:

Next Steps:

App.css 

You should add the CSS styles from the GitHub repository into your project’s App.css file. Here is the explanation for CSS styles:

Output

Run the command:

npm start

You should see that the application is started at localhost 3000, as shown below:

Here is our main web page:

Click on the upload file, and you will get the Filestack file picker under:

Upload the file from your desired location and click on the upload button. You will get the QR code generated for that specific file: 

Conclusion

It is easy and fast to add QR codes with Filestack into your React web application. Dynamic QR codes can be generated easily. They can be used for sharing product details, payment processing, or event ticketing. React integration with Filestack’s API is quick and efficient.

Filestack ensures security and submission are not overlooked. The QR code feature can be customized within the application. This guarantees both security and dependability.

Filestack simplifies development, whether for small or large applications. It allows you to focus on user needs without worrying about code complexity.

FAQs

What is QR code integration?

This enables users to scan codes for information or payments. Applications like Filestack and PayPal use this feature.

How do you integrate the QR code feature into a website?

Use a QR code generator API. Manage the API within your website’s code. Automate it to create and display codes based on user interaction.

Does Filestack provide secure QR code generation?

Filestack offers secure QR code generation. It uses encryption and access procedures to protect data. It checks the file’s availability before creating the QR code.

How much do the Filestack QR codes feature cost?

Filestack-paid plans start at $69 per month. You can always upgrade to higher plans. 

 

Sign Up for free at Filestack to create an app that generates accurate QR codes. 

Exit mobile version