Clariity / react-chessboard

The React Chessboard Library used at ChessOpenings.co.uk. Inspired and adapted from the unmaintained Chessboard.jsx.
https://react-chessboard.vercel.app
MIT License
335 stars 101 forks source link

Programmatically clear arrows from board #143

Closed Epitomaniac closed 2 months ago

Epitomaniac commented 3 months ago

Need a feature where I can call a function to remove arrows from the board; at the moment, the only way to clear arrows without changing the position is to click on the board; calling setCustomArrows([]) inside onArrowsChange does nothing. There's probably a workaround, but haven't been able to wrap my head around it. It seems like the arrows drawn on the board are a separate entity from customArrows property of the board component, which can create problems when you want to create an implementation for saving arrow data to some database and displaying them live at the same time. Thanks.

Epitomaniac commented 2 months ago

Minimal example:

const Component = () => {
  const [arrows, setArrows] = useState([]);
  const [game, setGame] = useState(new Chess());

  const clearArrows = () => {
    setArrows([])
  };

  return (
    <div>
      <button onClick={clearArrows }>clear arrows</button>
      <Chessboard
        position={game.fen()}
        areArrowsAllowed={true}
        customArrows={arrows}
      />
    </div>
  );
};

After drawing arrows by hand, setArrows() can't directly manipulate them. This creates issues when you want to add further functionalities to the arrows, like updating their colors by holding shift and redrawing them, etc.