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

Bug: optimisticState is updated with both new state and optimisticValue in useOptimistic #31944

Open
vedanttikku12 opened this issue Dec 31, 2024 · 3 comments
Labels

Comments

@vedanttikku12
Copy link

vedanttikku12 commented Dec 31, 2024

Using the react Example given for useOptimistic hook in docs, an update in state passed to the hook should directly reset the optimisticState in one render.

Instead

  1. It calls the updaterFn first, updates the optimisticState using new state and optimisticValue during one render cycle
    resets optimisticState with new State in another render cycle.
  2. As a result, both the new state and optimisticValue is rendered. Tried this in 20x slowdown
React.19.issue.mov

React version: 19

Steps To Reproduce

  1. Throttle CPU to 20x slowdown
  2. Type some input in text box and press submit
  3. See that the optimistic state is displayed and then it is pushed down for an instant along with the new state
  4. Check console logs
Screen Shot 2024-12-31 at 1 42 21 PM

Link to code example:

https://codesandbox.io/p/sandbox/react-dev-forked-gmlxnr?file=%2Fsrc%2FApp.js&workspaceId=ws_QiCvK4c476hege6EDsXfpC

The current behavior

When new state is passed to useOptimistic,

  1. It updates optimisticState by calling updaterFn with new state and optimisticValue
  2. Component is rendered using this optimisticState (incorrect one)
  3. Another update to optimisticState is done by resetting it to the new state
  4. Component is rendered using this new optimisticState (correct one)

The expected behavior

When new state is passed to useOptimistic, it should directly reset the optimisticState without calling the updater Fn

@JorensM
Copy link

JorensM commented Dec 31, 2024

Does happen to me as well on the provided CodeSandbox. The change between the optimistic state back to the original state does indeed occur with 2 renders. I guess it should be just 1 render probably.

@JorensM
Copy link

JorensM commented Dec 31, 2024

the state gets updated but the optimistic state still has the pending data which only clears in the next render.

useOptimistic

@Ajay-25
Copy link

Ajay-25 commented Jan 2, 2025

Yes, This is happening for me as well
After receiving the actual results from the server and updating the messages in App component, the updateFn of useOptimistic runs again with the final updated state and optimisticValue ( which is now already part of the final state ), and returns the optimisticState with duplicate values ( one already part of the final state and one with sending:true ) for one render cycle,
but somehow ( Magically ) it renders with the correct state in the next render cycle ( Not sure how it identifies the duplicate value )

ref - https://codesandbox.io/p/sandbox/react-dev-forked-gmlxnr?file=%2Fsrc%2FApp.js&workspaceId=ws_QiCvK4c476hege6EDsXfpC as mentioned above

Another similar sandbox - https://codesandbox.io/p/sandbox/react-19-useoptimistic-3f7htj?file=%2Fsrc%2FApp.js%3A52%2C5&workspaceId=ws_PszjWaELh3PS49RV1rnLom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants