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:
BarcodeGeneratorSPFx

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. export default class SampleBarcode extends React.Component<ISampleBarcodeProps, {}> {
  7. public render(): React.ReactElement<ISampleBarcodeProps> {
  8. let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
  9. return (
  10. <div className={styles.sampleBarcode}>
  11. <div className={styles.container}>
  12. <div className={styles.row}>
  13. {numbers.map((val) => {
  14. return (<div className={styles.column}><Barcode value={val} ></Barcode></div>);
  15. })}
  16. </div>
  17. </div>
  18. </div>
  19. );
  20. }
  21. }
Demo