willb335 / chessboardjsx

:black_square_button: Chessboard built for React
https://chessboardjsx.com
MIT License
267 stars 79 forks source link

Redo after undo #48

Open szpakowski opened 4 years ago

szpakowski commented 4 years ago

I am having a similar problem compared to #35

Essentially if I undo a move and then want to redo the move, the position does not update to the new position.

Please see https://codesandbox.io/s/chessboardjsx-forward-back-buttons-nlmzn for working code and stackoverlfow issue https://stackoverflow.com/questions/57447692/chessboardjsx-undo-and-redo-moves-are-not-updating-when-changing-direction

szpakowski commented 4 years ago
// Check if there is a new position coming from props or undo is called
if (!isEqual(positionFromProps, previousPositionFromProps) || undoMove) {
  this.setState({
    previousPositionFromProps: previousPositionFromProps,
    undoMove: false
  });

The problem seems to stem from the fact that the previous position is being stored.

I changed the above to:

// Check if there is a new position coming from props or undo is called
if (!isEqual(positionFromProps, previousPositionFromProps) || undoMove) {
  this.setState({
    previousPositionFromProps: previousPositionFromProps,
    undoMove: false,
    currentPosition: positionFromProps
  });

The issue I was having was directly related to trying to go to the most recent position. Note that doing the above will cause undo to fail from the available demos. Personally, I would remove this feature and keep it a pure implementation of chess board position only (as stated in the description).

kevinludwig commented 4 years ago

I just ran into this and came to report the issue. I'm simply trying to manage fen outside the chessboard and pass it in (implementing move-back feature) and moving back one move just doesn't work. Moving back two moves does.

Also I don't understand how this undo feature would be usable in any scenario. If I have a PGN game player for example, I want a back button feature to go back. This feature can only allow back one move, and actively blocks me from implementing a real back button. Can there at least be a way to opt out of this behavior?