Barcode Generator in SPFx WebPart

Introduction

 
This blog will help you use react-barcode in the SPFx web part with React as a framework to generate a barcode.
 
React-barcode can help us generate barcodes with many different barcode formats, such as:

EAN / UPC , CODE 128, CODE 39, ITF-14, MSI, CODABAR, PHARMACODE
 
To check out the code for react-barcode, click on the link below:
 
https://github.com/kciter/react-barcode
 
To download the sample SPFx code:
 
 

Installation

 
To install react-barcode in your SPFx solution, please use the below command:
  1. npm i react-barcode  
Code for react-barcode:
 
Import the reference of react-barcode in the file where we it is required to generate the barcode.
  1. import * as Barcode from "react-barcode";   
To generate the barcode, add the below code in the render method:
  1. <Barcode value={valueForGeneratingBarcode} ></Barcode>   
Complete Web part Code
  1. import * as React from 'react';  
  2. import styles from './SampleBarcode.module.scss';  
  3. import { ISampleBarcodeProps } from './ISampleBarcodeProps';  
  4. import { escape } from '@microsoft/sp-lodash-subset';  
  5. import * as Barcode from "react-barcode";  
  6.   
  7. export default class SampleBarcode extends React.Component<ISampleBarcodeProps, {}> {  
  8.   public render(): React.ReactElement<ISampleBarcodeProps> {  
  9.     let numbers = [1, 2, 3, 4, 5, 6, 7, 8];  
  10.     return (  
  11.       <div className={styles.sampleBarcode}>  
  12.         <div className={styles.container}>  
  13.           <div className={styles.row}>  
  14.             {numbers.map((val) => {  
  15.               return (<div className={styles.column}><Barcode value={val} ></Barcode></div>);  
  16.             })}  
  17.           </div>  
  18.         </div>  
  19.       </div>  
  20.     );  
  21.   }  
  22. }   
Demo