Skip to content

[React 19] Bug: optimisticState is updated with both new state and optimisticValue in useOptimistic #31944

Open
@vedanttikku12

Description

@vedanttikku12

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions