React Chessboard Component

5.10.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates basic setup of the Chessboard component, integrating with `chess.js` for game logic to handle piece drops, update the game state, and display the FEN.

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;

view raw JSON →