* fix: Stale closure and unsafe parsing in usePersistedState
- Compute functional updates from the latest state via the setStoredValue
updater instead of a value captured in the setter's closure, and keep the
setter's identity stable across value changes so it is safe to use in
dependency arrays.
- Correct the setter's type so functional updates are typed as (prev: T) => T
rather than (value: T) => void, and surface them in the returned tuple via
Dispatch<SetStateAction<T>>.
- Guard the cross-tab storage listener against malformed JSON so a bad value
written under the same key can no longer throw, and reset to the default
value when the key is removed in another tab.
- Skip the redundant re-sync from storage on initial mount, which caused an
extra render for object values.
- Add tests covering persistence, functional updates, setter stability,
cross-tab sync, and key changes.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015VPRjPcmF3CMo1a3jUv1FV
* chore: Remove usePersistedState test file
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015VPRjPcmF3CMo1a3jUv1FV
* fix: Keep state updater pure in usePersistedState
Move persistence out of the setStoredValue updater into the setter itself,
computing functional updates from a ref that mirrors the latest state. React
may invoke updaters more than once, so they must be free of side effects.
---------
Co-authored-by: Claude <noreply@anthropic.com>