React Barcode Component
react-barcode is a React component that streamlines the process of generating various barcode types within a React application. It acts as a wrapper around the robust JsBarcode library, abstracting away the direct DOM manipulation typically required for barcode rendering. The package is currently stable at version 1.6.1 and has seen active development, with recent minor releases adding support for more barcode formats like CODE128 (including GS1-128/EAN-128, CODE128A, B, and C) and minor type fixes. Its release cadence appears to be driven by feature additions and maintenance rather than strict time intervals. Key differentiators include its simple component-based API for React developers, comprehensive support for numerous barcode formats inherited from JsBarcode, and built-in TypeScript type definitions for enhanced developer experience.
Common errors
-
TypeError: Barcode is not a function
cause Incorrect import statement for the Barcode component.fixEnsure you are using `import Barcode from 'react-barcode';` for ESM or `const Barcode = require('react-barcode');` for CommonJS, as `Barcode` is a default export. -
The barcode value '...' is invalid for the format '...' (in browser console)
cause The input value provided to the Barcode component does not conform to the specifications of the selected barcode format.fixReview the `value` and `format` props. Ensure the `value` matches the expected pattern (e.g., number of digits, character set) for the `format`. Consult the JsBarcode documentation for format-specific requirements. -
TS2322: Type 'string | undefined' is not assignable to type 'string'.
cause Attempting to pass an optional or potentially undefined prop (e.g., `value` or `format`) to a property that expects a non-nullable string, without proper type guards.fixEnsure that required props like `value` are always provided as a `string`. For optional props, provide a default value or use optional chaining/nullish coalescing if the underlying `JsBarcode` library handles `undefined` gracefully, or cast if confident in runtime type.
Warnings
- gotcha The `react-barcode` component is a wrapper around `JsBarcode`. All options passed to the `Barcode` component directly map to `JsBarcode` options. Familiarity with `JsBarcode`'s documentation, particularly regarding format-specific options and value requirements, is crucial for correct barcode generation.
- gotcha Providing an invalid `value` for a selected `format` (e.g., non-numeric data for EAN, incorrect length) will result in an unrenderable or visually corrupted barcode, often appearing as a blank space or an SVG with an error message in debug mode.
- gotcha Starting from version 1.5.3, the `ean128` property (related to GS1-128/EAN-128 format) was changed to be optional in the TypeScript definitions. If your project relied on this property always being present or strictly required, this change might affect type-checking.
Install
-
npm install react-barcode -
yarn add react-barcode -
pnpm add react-barcode
Imports
- Barcode
import { Barcode } from 'react-barcode';import Barcode from 'react-barcode';
- Barcode (CommonJS)
const Barcode = require('react-barcode'); - BarcodeProps
import type { BarcodeProps } from 'react-barcode';
Quickstart
import React, { FunctionComponent } from 'react';
import Barcode from 'react-barcode';
interface MyBarcodeGeneratorProps {
dataValue: string;
barcodeFormat?: string;
}
export const MyBarcodeGenerator: FunctionComponent<MyBarcodeGeneratorProps> = ({ dataValue, barcodeFormat = "CODE128" }) => {
return (
<div>
<h1>Generated Barcode:</h1>
<Barcode
value={dataValue}
format={barcodeFormat}
width={2}
height={100}
displayValue={true}
lineColor="#333333"
background="#ffffff"
margin={10}
/>
<p>Scan this barcode!</p>
</div>
);
};
// Example usage:
// function App() {
// return <MyBarcodeGenerator dataValue="YOUR_DATA_HERE" barcodeFormat="EAN13" />;
// }