Skip to content

Conversation

kyukyu-dev
Copy link

…r and prevent stale closure

Overview

This PR refactors useControlledState to improve internal stability and prevent stale closures.

  • Stores the latest onChange callback in a ref (onChangeRef).
  • Uses useInsertionEffect (or useLayoutEffect as a fallback) to synchronize the ref update before layout effects.
  • Prevents stale closures in setValue and remove onChange from dependency arrays to avoid unnecessary re-renders.

This change aligns the hook's behavior with React 18's Strict Mode and concurrent rendering patterns.

Checklist

  • Did you write the test code?
  • Have you run yarn run fix to format and lint the code and docs?
  • Have you run yarn run test:coverage to make sure there is no uncovered line?
  • Did you write the JSDoc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant