Skip to content
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

Bug: useState is not getting updated inside Event Listener (Webrtc DataChannel). #29860

Open
michaelnadar opened this issue Jun 11, 2024 · 2 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@michaelnadar
Copy link

michaelnadar commented Jun 11, 2024

useState is not getting updated inside Event Listener.
inside it stores previous value
I'm developing simple tiic-tac-toe multi player react app using webrtc datachannel
where when i reset the game the peer game should have to be reset at that moment but inside the event it has some other value stored (previous)
React version:18.2.0

this is an rtcdatachannel event call
const receiveChannelCallback = (event) => {
receiveChannel.current = event.channel;
if(receiveChannel.current.label === "mouseChannel"){
console.log('inside mouse channel receive side');
handleMouseChannel(receiveChannel.current);
}else if(receiveChannel.current.label === "messageChannel"){
receiveChannel.current.onmessage = handleDChat;
}else if(receiveChannel.current.label === "gameChannel"){
receiveChannel.current.onmessage = handleDGame;

  }else if(receiveChannel.current.label === "resetChannel"){
  receiveChannel.current.onmessage = handleDReset;
  }
}

  receivePc.ondatachannel = receiveChannelCallback;

const handleDGame = (e) => {
console.log(matrix);
const { data } = JSON.parse(e.data);
const { preVal, r, c } = data;

if (matrix[r][c] === "-") {
  setDisable(false);

  // Update preVal
  const newPreVal = preVal === "X" ? "O" : "X";
  setPreVal(newPreVal);

  // Use functional update to ensure the latest state
  setMatrix((prevMatrix) => {
      const newMatrix = [...prevMatrix];
      newMatrix[r][c] = preVal;
      
      const winner = checkWinner(newMatrix);
      if (winner) {
          setWinner(true);
          setWinVal(winner);
          setDraw(false);
          setDisable(true);
      } else if (!handleDraw(newMatrix)) {
          setDraw(true);
      }

      return newMatrix;
  });

}

Link to code example:

https://github.com/michaelnadar/tic_tac-tsc/blob/main/frontend-tsc/src/components/Room.tsx

The current behavior

inside handleDGame while i m resetting the matrix value to its initial val it stores prev val

The expected behavior

it has to update reset the matrix value inside handleDGame

@michaelnadar michaelnadar added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Jun 11, 2024
@nayelimdejesus
Copy link

I would like to work on this issue

@michaelnadar
Copy link
Author

@nayelimdejesus Yeah go on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants