fix(useControlledState): fix multiple function set state actions problem#344
Open
kyukyu-dev wants to merge 2 commits intotoss:mainfrom
Open
fix(useControlledState): fix multiple function set state actions problem#344kyukyu-dev wants to merge 2 commits intotoss:mainfrom
kyukyu-dev wants to merge 2 commits intotoss:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR improves useControlledState to correctly handle multiple functional setState actions executed within the same render cycle.
Previously, functional updates relied on a closure-captured value, which could lead to stale state when multiple updates were batched by React. This change introduces an internal valueRef to always compute the next state from the latest intermediate value.
Additionally, a safe insertion/layout effect is used to keep the ref synchronized with the actual state after each render, ensuring consistent behavior in both controlled and uncontrolled modes.
Problem
useControlledStatepreviously calculated the next value using the value captured in the closure.const nextValue = isSetStateAction(next) ? next(value) : next;When multiple functional setState actions were executed within the same render cycle, each updater received the same stale value, because React batches updates.
Example:
Both updates used the same previous value (5).
Solution
To ensure each updater receives the latest intermediate value, this PR introduces an internal valueRef.
const valueRef = useRef(uncontrolledState);The setter now computes updates based on valueRef.current:
const nextValue = isSetStateAction(next) ? next(valueRef.current) : next;After computing the next value, the ref is updated immediately:
valueRef.current = nextValue;This ensures subsequent functional updates in the same render cycle receive the correct latest value.
Ref synchronization
Since the ref may be optimistically updated before React commits the actual value, we resync it after each render using a safe insertion/layout effect:
This keeps the ref consistent with the real state value.
Controlled mode edge case
In controlled mode, the parent may choose not to update the value.
To ensure the ref resynchronizes correctly in that case, a forced re-render is triggered:
Result
useControlledState now correctly handles multiple functional updates:
without stale closure issues.
Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?