-
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
Bug: Stale values returned from useOptimistic when state changes #27617
Comments
I'm experiencing the same issue. |
Me too. Downgrading to next 13 solves it for now |
I'm experiencing the same issue. |
same issue here. |
same here, for me it sometimes flashes the correct value before changing back to the original value |
Same here. It's not more experimental version, why is this happening? |
For me it does every time |
2024-01-11.00-42-19.mp4` import { addTodo } from "@/actions/todo"; type Props = { const Form = ({ todos }: Props) => {
} export default Form |
I think this is an expected behaviour of useOptimistic. Or rather, the optimistic update is replaced with the new state as soon as the transition is over. When there is no async work in the transition, the optimistic update is replaced instantly. Edit: In fact it says exactly that in the first line of the docs (emphasis my own):
It's expected for useOptimistic to essentially do nothing outside of async transitions. |
This is, indeed, a bug — I apologize for not looking into this until now (thanks @tom-sherman for the ping on Should be fixed by #27936 |
The fix has now landed in the most recent React and Next.js canaries. Thanks for the bug report! |
The
optimisticState
returned fromuseOptimistic
is stale when thestate
param passed touseOptimistic
is changed. The change could come from an RSC reponse or from asetState
.React version: 18.2.0
Steps To Reproduce
useOptimistic
with astate
variablestate
either viasetState
or any other method that would cause a rerenderstate
is returned as theoptimisticState
Link to code example: https://github.com/dorshinar/next-optimistic-bug
I've recreated the bug in a Next project, but the important component is this:
The current behavior
When there is no form submission in progress, the
optimisticArr
always returns the initialstate
provided to it.The expected behavior
I'd exepct that
optimisticState
returned fromuseOptimistic
to be in sync with thestate
param passed to it.The text was updated successfully, but these errors were encountered: