-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Board resets to previous state despite value of position
#119
Comments
position
Notably this only happens at the start, once remoteGame updates or I move a piece locally to update the local state, it updates to the correct visual state. |
I just managed to mitigate this issue by disabling React.StrictMode, it seems like the double update is what was making this issue appear. Good that it surfaced it, but not good bc I don't know how to fix the underlying issue. |
Instead of:
can you try how it is done in the examples?:
then updating the game class and using the functionality within that, instead of updating the game position and class separately |
That was the first thing I tried, same issue. jfyi that code with useMemo is also from the examples (that comes from the storyboard, plus some of the other issues in this repo). I can provide a full repro later. |
Hi @banool ! useEffect(() => {
console.log("blah");
if (remoteGame === undefined) {
return;
}
console.log("setting");
setChessBoardPosition(gameToFen(remoteGame));
}, [remoteGame, localGame, chessBoardPosition, setChessBoardPosition]); First of all, by directly calling localGame.load(gameToFen(remoteGame));
setChessBoardPosition(localGame.fen()); Secondly, please make sure that the dependency array of your useEffect doesn't contain extra dependencies, for example |
Here is a repro with the latest code. First, clone the code:
Run the dev site:
Open the site: http://localhost:3000/#/0xd81a98dab67b5bd2943e85dee7a6b8026837f09b63d0f86529af55601e2570b3?network=testnet You will see the pieces appear in the right spot and then snap back to the starting position. Disabling strict mode fixes this, implying some kind of bug that manifests only on running effects / render an extra time. As you can see, I just have The logging is pretty clear, the same FEN is passed into the Chessboard for both of the renders at the end, so it shouldn't behave this way. The relevant code from the repo: https://github.com/banool/aptos-chess/blob/main/frontend/src/pages/GamePage/MyChessboard.tsx. |
Love the library, so I hate to jump on this complaint bus, but I faced a similar (slightly different, but possibly related) issue just now. I'm creating a puzzle mode, and as with most puzzle modes, you have the first move being the opponent's move, and then you respond. There is a However, for anyone's future reference, I believe it may have something to do with how the Context component takes a while to match the diffs between the current and previous boards. I did come up with a somewhat hacky solution - there is a ref (for removing premoves) where the I get that to get the nice piece-moving animations, it would be difficult to disable the diff-matching on the initial board. However, I would love to know if there are better ways of doing this. |
@banool I believe your issue has nothing to do with the react-chessboard library. Imo your issue comes from the fact that when you make a move, you are correctly setting the new board position in your |
I'm working on a project and ran into this issue too! It is indeed caused by React's Strict Mode which intentionally mounts everything twice to help you caught / find remounting bugs during development (on production it only mounts once). Disabling it is a workaround but obviously not ideal. The reason solution would be to find out why the library is failing to handle 2 quick re-renders correctly and then falling back to the initial position. I tried some 'hacky workarounds' but they caused other issues with animations / flickering. I'll try to look into it later if I get some time. |
Possibly but I doubt it. I setup a simple board and used React to update the fen binded to a prop on page load. Due to strict modes double render it caused the flickering. Sadly I didn't have time to investigate further and moved on. |
Updating board state in a useEffect isn't recommended, you should initialise a board state in a useState setter and update it with events, not side effects |
@Gen1Code if your app is open source or you can extract this part of your code to a public repo, I can take a look into it if you want. |
@Gen1Code Found your repo on your profile, I opened an issue over there with hints for where I think the issue comes from in your case. |
I have the following code (simplified to remove unrelated stuff):
The point of this code is to update the local state of the board based on the state of the game from a remote source.
The state updates seem to be correct, but the board doesn't seem to "persist" the state I give it. Instead, it shows it briefly and then resets back to the initial state. You can see what I mean in the recording.
Screen.Recording.2023-12-22.at.12.32.04.PM.mov
When logging to the console, I can see this:
This tells me I'm passing in the correct state of the game to my Chessboard.
I have tried removing the Flex and Box wrapping the Chessboard and that does nothing.
Setting a static boardWidth and removing that resizing hook doesn't help.
I have tried using just useState without useMemo but that doesn't help.
Given I'm passing in a certain FEN to Chessboard and it doesn't show it anyway, it tells me it is some kind of bug with the Chessboard, but I'm not too sure.
Any ideas on what I can do to fix this?
Versions:
The text was updated successfully, but these errors were encountered: