React Chessboard Component
React Chessboard is a modern, responsive, and accessible chessboard component for React applications, currently at stable version 5.10.0. It provides core functionality for rendering chessboards with drag-and-drop piece movement, custom piece rendering, animation, and comprehensive event handling. The library maintains a continuous release cadence, with frequent minor updates and bug fixes, often several times a month. Key differentiators include robust mobile support, built-in responsiveness, accessibility features, and full TypeScript support, ensuring type safety and improved developer experience. It requires React 19.0.0 as a peer dependency, aligning with the latest React ecosystem, making it suitable for modern React projects and providing a flexible foundation for building various chess-related user interfaces.
Common errors
-
Error: Cannot find module 'react-chessboard'
cause The package is not installed or the import path is incorrect, or a CommonJS environment is trying to `require` an ESM-only package.fixInstall the package: `npm install react-chessboard` (or `pnpm add react-chessboard`, `yarn add react-chessboard`). Ensure you are using `import { Chessboard } from 'react-chessboard';` in an ESM-compatible environment or build setup. -
Warning: Peer dependency 'react@^19.0.0' not installed.
cause Your project's `react` version does not satisfy the `react-chessboard` peer dependency requirement.fixUpdate your `react` and `react-dom` packages to version `19.0.0` or newer: `npm install react@^19.0.0 react-dom@^19.0.0`. -
Invariant Violation: Hydration failed because the initial UI does not match what was rendered on the server.
cause Typically occurs in Next.js or similar SSR frameworks when a component that relies on client-side browser APIs (like `react-chessboard` for drag-and-drop) is rendered on the server without proper client-side directives.fixAdd `'use client';` at the top of the file where `Chessboard` is rendered, or use `next/dynamic` to import `Chessboard` with `ssr: false`.
Warnings
- breaking Version 5.x and above requires React 19.0.0 as a peer dependency. Applications using older React versions (e.g., React 18) must upgrade their React installation to use the latest `react-chessboard` features.
- gotcha When using `react-chessboard` within Next.js or other server-side rendering (SSR) frameworks, the component must be rendered on the client side. Failing to do so can lead to hydration mismatches or errors.
- gotcha The `engines` field in `package.json` specifies minimum Node.js (>=20.11.0) and pnpm (>=9.4.0) versions. Using older versions may lead to installation or runtime issues.
- gotcha Prior to v5.9.1, the `onClick` handler could unexpectedly fire on mobile devices when attempting to drop pieces, leading to unintended moves or UI behavior.
Install
-
npm install react-chessboard -
yarn add react-chessboard -
pnpm add react-chessboard
Imports
- Chessboard
const Chessboard = require('react-chessboard');import { Chessboard } from 'react-chessboard'; - ChessboardProps
import type { ChessboardProps } from 'react-chessboard'; - SquareRenderer
import type { SquareRenderer } from 'react-chessboard';
Quickstart
import { useState, useCallback } from 'react';
import { Chessboard } from 'react-chessboard';
import { Chess } from 'chess.js'; // Often used for game logic
function App() {
const [game, setGame] = useState(new Chess());
const onDrop = useCallback((sourceSquare: string, targetSquare: string) => {
try {
const move = game.move({
from: sourceSquare,
to: targetSquare,
promotion: 'q' // For simplicity, always promote to queen
});
if (move === null) return false; // Illegal move
setGame(new Chess(game.fen())); // Update state to trigger re-render
return true; // Valid move
} catch (e) {
console.error("Error making move:", e);
return false; // Error (e.g., illegal move)
}
}, [game]);
return (
<div style={{ maxWidth: '400px', margin: 'auto', padding: '20px' }}>
<h1>React Chessboard Simple Game</h1>
<Chessboard position={game.fen()} onPieceDrop={onDrop} />
<p style={{ marginTop: '10px' }}>Current FEN: {game.fen()}</p>
<button style={{ marginTop: '10px' }} onClick={() => setGame(new Chess())}>Reset Game</button>
</div>
);
}
export default App;