React Spreadsheet
React Spreadsheet is a library for building simple, customizable, and performant spreadsheet components within React applications. As of version 0.10.1, it focuses on providing a straightforward API for common use cases while maintaining flexibility. It has seen consistent updates, with recent releases introducing a new formula evaluation engine that supports cell dependencies and ranges (v0.8.0), and features like row/column selection (v0.7.0). The library differentiates itself by emphasizing 'Just Components™' and aiming for performance without virtualization, making it suitable for scenarios where a fully virtualized, complex spreadsheet might be overkill. It ships with TypeScript type declarations, ensuring a better developer experience with type introspection.
Common errors
-
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
cause Incorrectly importing the Spreadsheet component as a default import instead of a named import.fixChange the import statement from `import Spreadsheet from 'react-spreadsheet'` to `import { Spreadsheet } from 'react-spreadsheet'`. -
TypeError: (0 , _reactSpreadsheet.Spreadsheet) is not a function
cause Attempting to use CommonJS `require()` syntax with a library primarily designed for ES modules or attempting to destructure a default export that doesn't exist.fixUse ES module named import syntax: `import { Spreadsheet } from 'react-spreadsheet';`. If CommonJS is strictly necessary, ensure you are correctly destructuring the named export: `const { Spreadsheet } = require('react-spreadsheet');` -
Uncaught Error: The current evaluation engine does not support formula dependencies yet for cell A1. This usually indicates a formula like '=B1+C1' where B1 is also a formula.
cause This error message indicates an older version of `react-spreadsheet` that lacks the advanced formula evaluation engine introduced in v0.8.0.fixUpgrade `react-spreadsheet` to version `0.8.0` or higher to utilize the new formula evaluation engine that supports cell dependencies and ranges.
Warnings
- breaking The `getBindingsForCell` prop has been removed to facilitate the new formula evaluation engine. This prop is no longer available in the component API.
- breaking The `Row` component was split into two distinct components: `Row` (for table body rows) and `HeaderRow` (for the table header row). This impacts custom row rendering.
- deprecated The `CellDescriptor` type was deprecated in version 0.5.15. This change was part of an internal overhaul to use React Context and Reducer for state management, replacing Unistore, to support newer React versions.
- gotcha React Spreadsheet requires `react`, `react-dom` (both >=16.8.0), and `scheduler` (>=0.19.0) as peer dependencies. Missing or incompatible versions can lead to runtime errors or instability.
Install
-
npm install react-spreadsheet -
yarn add react-spreadsheet -
pnpm add react-spreadsheet
Imports
- Spreadsheet
import Spreadsheet from 'react-spreadsheet'
import { Spreadsheet } from 'react-spreadsheet' - CellBase
import { CellBase } from 'react-spreadsheet' - DataEditor
import { DataEditor } from 'react-spreadsheet'
Quickstart
import React, { useState } from 'react';
import { Spreadsheet, CellBase } from 'react-spreadsheet';
interface MyData extends CellBase {
value: string;
}
const initialData: MyData[][] = [
[{ value: 'Header 1' }, { value: 'Header 2' }, { value: 'Header 3' }],
[{ value: 'Row 1, Cell 1' }, { value: 'Row 1, Cell 2' }, { value: 'Row 1, Cell 3' }],
[{ value: 'Row 2, Cell 1' }, { value: 'Row 2, Cell 2' }, { value: 'Row 2, Cell 3' }],
[{ value: '=SUM(A2:B2)' }, { value: '=A2*B2' }, { value: 'Example Formula' }]
];
function MySpreadsheetApp() {
const [data, setData] = useState<MyData[][]>(initialData);
const handleDataChange = (newData: MyData[][]) => {
console.log('Spreadsheet data changed:', newData);
setData(newData);
};
return (
<div style={{ padding: '20px', fontFamily: 'sans-serif' }}>
<h1>Simple React Spreadsheet</h1>
<p>Edit cells, including basic formulas like =SUM(A2:B2).</p>
<Spreadsheet data={data} onChange={handleDataChange} />
</div>
);
}
export default MySpreadsheetApp;