React Sparklines (TypeScript Rewrite)
The `react-sparklines` package (maintained by acodesmith) is a modern, TypeScript-rewritten version of the original unmaintained `react-sparklines` library. It provides a suite of React components for rendering small, expressive sparklines, including various visual elements like lines, bars, spots, reference lines, and normal bands. As of version 1.3.0, this fork actively uses modern React patterns such as functional components, and ships with comprehensive TypeScript types, offering a smaller, faster, and more actively maintained alternative to its predecessor. Its key differentiators include explicit TypeScript support, adherence to contemporary React development practices, and a focus on performance and bundle size. The release cadence is driven by ongoing modernization and bug fixes, providing a stable, up-to-date solution for compact data visualization in React applications.
Common errors
-
TypeError: (0 , react_sparklines__WEBPACK_IMPORTED_MODULE_2__.Sparklines) is not a function
cause This error typically indicates that a named export (like `Sparklines`) is being incorrectly imported, often by attempting a default import or a faulty CommonJS `require()` pattern in a module-aware environment.fixEnsure you are using named imports correctly: `import { Sparklines, SparklinesLine } from 'react-sparklines';` -
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)
cause This issue often arises when React components are not correctly imported or are used in a React version that does not support the component's underlying implementation (e.g., trying to use functional components with an ancient React setup), or if there's a module resolution problem.fixVerify that `react` and `react-dom` are installed as peer dependencies and that your project uses a modern React version (16.8 or newer). Double-check that your import paths are correct and that the component names are spelled exactly as exported: `import { Sparklines } from 'react-sparklines';`
Warnings
- breaking This `react-sparklines` package (version 1.3.0 and above, maintained by `acodesmith`) is a complete rewrite in TypeScript using modern React functional components. If you are migrating from the older `borisyankov/react-sparklines` package, expect significant API changes and ensure your codebase is compatible with modern React patterns.
- gotcha Despite the internal or conceptual name `react-sparklines-typescript`, this library is published on npm under the name `react-sparklines`. To ensure you install this modern, TypeScript-rewritten version, install `react-sparklines` and verify the maintainer (`acodesmith`) to distinguish it from the older, unmaintained package with the same name.
- gotcha The library is designed for ESM (ECMAScript Modules) and modern JavaScript environments with bundlers like Webpack or Vite. While CommonJS `require()` might be polyfilled in some setups, it is not the recommended or officially supported way to import components, and may lead to issues with tree-shaking, type inference, or bundle size.
- gotcha The `width` and `height` props for the `Sparklines` container define the SVG viewbox dimensions, and the component will automatically scale responsively within its parent container by default. If you desire absolute pixel dimensions, you must use `svgWidth` and `svgHeight` props instead.
Install
-
npm install react-sparklines-typescript -
yarn add react-sparklines-typescript -
pnpm add react-sparklines-typescript
Imports
- Sparklines
const Sparklines = require('react-sparklines').Sparklines;import { Sparklines } from 'react-sparklines'; - SparklinesLine
import SparklinesLine from 'react-sparklines/SparklinesLine';
import { SparklinesLine } from 'react-sparklines'; - SparklinesReferenceLine
const { SparklinesReferenceLine } = require('react-sparklines');import { SparklinesReferenceLine } from 'react-sparklines'; - SparklinesBars
import { SparklinesBars } from 'react-sparklines';
Quickstart
import React from 'react';
import { Sparklines, SparklinesLine, SparklinesSpots, SparklinesReferenceLine } from 'react-sparklines';
// A simple functional component demonstrating sparklines
const MySparklineChart: React.FC = () => {
const sampleData = [5, 10, 5, 20, 8, 15, 12, 18, 7, 13, 10, 22];
const meanValue = sampleData.reduce((a, b) => a + b, 0) / sampleData.length;
return (
<div style={{ width: '200px', height: '50px', border: '1px solid #ccc', padding: '10px', margin: '20px' }}>
<h3 style={{ fontSize: '1em', marginBottom: '5px' }}>Sales Trend</h3>
<Sparklines data={sampleData} width={180} height={40} margin={5}>
<SparklinesLine color="#1a73e8" style={{ fill: 'none', strokeWidth: 2 }} />
<SparklinesSpots style={{ fill: '#1a73e8' }} size={2} />
<SparklinesReferenceLine type="mean" style={{ stroke: 'red', strokeDasharray: '2, 2' }} />
</Sparklines>
<p style={{ fontSize: '0.8em', color: '#555', marginTop: '5px' }}>Mean: {meanValue.toFixed(2)}</p>
</div>
);
};
export default MySparklineChart;