-
Notifications
You must be signed in to change notification settings - Fork 46.4k
-
Notifications
You must be signed in to change notification settings - Fork 46.4k
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
[custom renderer] weird behavior of the useState hook #16171
Comments
Looks like you need to retrieve the latest state value for counter: setCounter(counter => counter + 1) |
@graphicbeacon hm ... but why I have to do that. I mean this may fix the problem but I'll expect when calling |
I think you forgot to assign the new onClick callback to the dom node when the function's reference changes. So when you click on your span, the function created in the first render pass get's called. Therefore it doesn't matter what the value of the counter is, It's always 0 in the closure of the onClick function. |
@Eddie-CooRo you nailed it. That was the reason. I had to basically amend my |
Do you want to request a feature or report a bug?
not a bug nor a feature (I probably missed something in my custom renderer host config)
What is the current behavior?
I have the following example:
All works fine until I click on the
XXX(42)
text. This is suppose to update the local state to43
but it sets it to1
. And then no matter how many times I click it stays1
. I assume that it gets the initial value of0
on ever render.Here's the code of the example https://github.com/krasimir/react-custom-renderer-experiment/blob/master/src/index.js together with my custom host config. The same example works just fine using ReactDOM renderer. I tried to cover as many methods as possible and can't figure out why
useState
is not behaving correctly.What is the expected behavior?
I expect to get the updated state value out of the
useState
call.Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
I'm testing under macOS, Chrome 75.
The text was updated successfully, but these errors were encountered: