-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
useImperativeHandle behaves as if inputs are [] by default #14782
Comments
That's a pretty common "bug" caused by Javascript closures. React apparently is not recreating handle on every render so it will always see only the value from the first render. You can work around that by using ref: https://codesandbox.io/s/8nr4x36zy9 Personally, I would recommend you to avoid useImperativeHandle. It should be used sparsely for a reason. There are definitely better ways for a data flow. |
I think this kind of problems should get its own section in the docs. Many people will get burned by this (me included) because it's just not that apparent without knowing this advanced JS behavior. |
But why work in alpha version? |
@FredyC I’m not use it to transfer value this just example To control the state of expanded or collapsed |
I think this is a mindset issue, possibly coming from how jQuery does the things. You shouldn't be controlling components by calling methods on them. Instead, you should pass props that will change that state. I don't have a capacity to update that example of yours right now. Just try to fiddle with it and I am sure you will figure a way without imperative access. |
There's a third parameter to The bug here (and I do think it's a bug) is this: unlike the other hooks that have an This does mean there's an easy workaround for @AhmedElywa or anyone else hitting this issue: just pass something to |
Here's the first example as updated with workaround: https://codesandbox.io/s/nnrlpxx1qm - note line 13 of Test.js. With that it shows the current value as expected. |
The third argument to useImperativeHandle(
ref,
() => ({
getValue: () => {
console.log(value);
}
}),
[value]
); Working sandbox here: https://codesandbox.io/s/zl8zy0l6jx |
@MicahChalmer and @hamlim thanks for explain |
@hamlim I think that @MicahChalmer was pointing out correctly, that unlike the |
Oh wow I must have completely skipped over @MicahChalmer's comments above mine 🤦♂️ sorry about that! |
Sounds like a bug. |
Canary |
Fixed in 16.8.2. |
useImperativeHandle have bug in 16.8.1 if you want to access any state will get the initial state not current state but in 16.8.0-alpha.1 it work good
You can see here https://codesandbox.io/s/xjl8znwpz open console when you change input value will get the currently value in console but if you clicked button get value from ref will get initial value (e)
If you try same here https://codesandbox.io/s/qxkll6po0j with 16.8.0-alpha.1 when clicked button get currently value
The text was updated successfully, but these errors were encountered: