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: Both optimisticValue and new state is displayed together using useOptimistic #31943

Closed
vedanttikku12 opened this issue Dec 31, 2024 · 1 comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@vedanttikku12
Copy link

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
  2. resets optimisticState with new State in another render cycle.

As a result, both the new state and optimisticValue is rendered. Tried this in 20x slowdown

Screen.Recording.2024-12-31.at.1.30.15.PM.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 28 14 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

@vedanttikku12 vedanttikku12 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Dec 31, 2024
@vedanttikku12
Copy link
Author

#31944 Added this same issue here with React-19 label. Closing this as duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

1 participant