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 in rerenderOptimistic: Incorrect mount-or-update check order leads to improper hook state handling #32220

Open
Aatirhassanpir opened this issue Jan 24, 2025 · 0 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@Aatirhassanpir
Copy link

Description

In the rerenderOptimistic function of React, there is a bug where the order of operations is incorrect when determining whether a component is mounting or updating. Specifically, the check for currentHook !== null is performed before calling updateWorkInProgressHook, which causes improper handling of hook state during optimistic updates.

React version

React 18.x (or specify your version if different)

###Steps to Reproduce:
Create a React component that uses the useOptimistic hook for managing optimistic state.
Perform a state update inside the render phase (e.g., inside useEffect or useCallback).
Observe that the hook's state and update behavior are incorrect, leading to inconsistent renders.
This issue typically occurs when the useOptimistic hook is the first hook in the component, and there are updates or transitions that occur later in the render phase.

Link to Code Example:

[CodeSandbox Example or GitHub Repo with Minimal Example]

Current Behavior

The state update and hook processing are incorrect because currentHook is checked before updating it. This causes the component to incorrectly identify whether it is mounting or updating.

Expected Behavior

updateWorkInProgressHook should be called before checking whether currentHook is null, ensuring that the state is properly updated and the correct lifecycle phase (mount or update) is identified. This fix would result in proper optimistic update handling during re-renders.

@Aatirhassanpir Aatirhassanpir added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Jan 24, 2025
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