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

Cannot update state inside setInterval #351

Closed
defint opened this issue Aug 7, 2020 · 6 comments
Closed

Cannot update state inside setInterval #351

defint opened this issue Aug 7, 2020 · 6 comments
Labels
bug Something isn't working

Comments

@defint
Copy link

defint commented Aug 7, 2020

Hello
I have to change the atom value inside setInterval function.

My first version of the code:
https://gist.github.com/defint/3faffb974ab76b1f7aa8adf0ff2ebadb
It doesn't work.

My second version of the code with auto-clicking on the button:
https://gist.github.com/defint/fcf383dda69a934bfa411c2b89b72bc4
It works fine.

Can you please describe how to change the state without auto-clicking?

@artalar
Copy link
Owner

artalar commented Aug 7, 2020

@defint thx for the issue, it a pretty weird thing, but I m sure your App wrapped into React.StrictMode. If you will try to remove React.StrictMode the bug will go.
I still don't know why it happened, but let's keep this issue opened and I try to investigate and solve the bug a bit later.

@artalar artalar added the bug Something isn't working label Aug 7, 2020
@rrrshtt
Copy link

rrrshtt commented Sep 30, 2020

@artalar I found the problem, it lies in the way the react handles events. I will try to dive deeper into the topic, leave links that helped me.
facebook/react#14259
pmndrs/zustand#115
image

@rrrshtt
Copy link

rrrshtt commented Sep 30, 2020

@defint quick solution for you
useEffect(() => { setInterval(() => { ReactDOM.unstable_batchedUpdates(() => changeValue(Math.random())); }, 1000); }, []);

@lkostrowski
Copy link

Additional info from me - I'm using action dispatchin on websocket event, effectively every second, I also didn't get updates and also was fixed by removing react strict mode

@artalar
Copy link
Owner

artalar commented Feb 17, 2021

I will try to use unstable_batchUpdate + https://www.npmjs.com/package/use-subscription to fix this problem.
cc #214

@artalar
Copy link
Owner

artalar commented May 7, 2021

@artalar artalar closed this as completed May 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants