Closed dorshinar closed 8 months ago
It doesn't,t look like you're using the useOptimistic hook correctly there. (But keep in mind, last time I personally used it it was still experimental)
Maybe try to create a setup just like the official docs show.
https://react.dev/reference/react/useOptimistic
Ps. This is might be better suited in the react repo rather than the Next.js one
Thanks for the reply @JesseKoldewijn!
I might be using it wrong, but as I understand it my example should work. I've tried adding a simple echo function as the second parameter to useOptimistic
, but I get the same behavior
const [optimisticArr] = useOptimistic(arrFromServer, (s) => s);
My example intentionally does not use the callback returned from useOptimistic
to show that the issue is not about the optimistic update mechanism, but the internal state that useOptimistic
apparently holds.
Without a form submission in progress useOptimistic
should just pass the state
through to my app IIUC.
Thanks for the reply @JesseKoldewijn!
I might be using it wrong, but as I understand it my example should work. I've tried adding a simple echo function as the second parameter to
useOptimistic
, but I get the same behaviorconst [optimisticArr] = useOptimistic(arrFromServer, (s) => s);
My example intentionally does not use the callback returned from
useOptimistic
to show that the issue is not about the optimistic update mechanism, but the internal state thatuseOptimistic
apparently holds.Without a form submission in progress
useOptimistic
should just pass thestate
through to my app IIUC.
I will have to dig in on the exact behaviour of the useOptimistic hook again but I'll have a look for ya later todayđź‘Ť
I think this might indeed be a bug in React. Looking at the code for useOptimistic
, specifically this line:
https://github.com/facebook/react/blob/3e09c27b880e1fecdb1eca5db510ecce37ea6be2/packages/react-reconciler/src/ReactFiberHooks.js#L1416
It appears that when both queues are null (pendingQueue
and baseQueue
), which is, as I understand it, what happens when there are no submissions in progress, the returned value is simply the hook.memoizedState
which is, as I suspect, the initial state provided to the hook.
I'll open a bug report in the React repo.
Opened the bug report https://github.com/facebook/react/issues/27617
This issue has been fixed in React https://github.com/facebook/react/issues/27617
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
Link to the code that reproduces this issue
https://github.com/dorshinar/next-optimistic-bug
To Reproduce
npm run dev
useEffect
setting the stateTo illustrate the bug I have a state variable called
arrFromServer
in the<Form />
component, and I am passing it throughuseOptimistic
as if I was updating data fetched from the server optimistically. ThearrFromServer
in a placeholder for data fetched from a Server Component, and theuseEffect
is a way to simulate updating the data on the server and refetching it. In the image below, theoptimisticArr
remains constant with 3 elements, whilearrFromServer
grows in an intervalCurrent vs. Expected behavior
I expect
useOptimistic
to return the up-to-datestate
passed as parameter while a mutation is not in-flight. The current behavior seems to return the initialstate
provided touseOptimistic
.Verify canary release
Provide environment information
Which area(s) are affected? (Select all that apply)
Data fetching (gS(S)P, getInitialProps)
Additional context
No response