ref(utils): Add functional updater support to useSessionStorage#111015
Open
ref(utils): Add functional updater support to useSessionStorage#111015
Conversation
Support SetStateAction<T> (function updater) in useSessionStorage's setter, matching React's useState API. This prevents stale-state bugs when multiple sequential calls spread the same snapshot, since each updater receives the latest prev value.
| setState(prev => { | ||
| const next = | ||
| typeof valueOrUpdater === 'function' | ||
| ? (valueOrUpdater as (prev: T) => T)(prev) |
Member
There was a problem hiding this comment.
Why do we have to cast this?
Member
Author
There was a problem hiding this comment.
Cast is needed because TypeScript can't narrow SetStateAction<T> (T | ((prev: T) => T)) via typeof === 'function' when T could theoretically be a function type. Constraining T to exclude functions would fix the narrowing but results in a verbose generic signature for a general-purpose utility. Added a comment explaining the cast instead.
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is kicking off a free cloud agent to fix this issue. This run is complimentary, but you can enable autofix for all future PRs in the Cursor dashboard.
evanpurkhiser
approved these changes
Mar 18, 2026
Ensures removeItem goes through React's setState queue, so it can't race with a pending wrappedSetState updater that would write the value back to storage after removal.
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.

Summary
SetStateAction<T>(function updater) inuseSessionStorage's setter, matching React'suseStateAPIprevvalueSplit out from #110883 per review feedback.
Test plan
CI=true pnpm test static/app/utils/useSessionStorage.spec.tsx-- 6 tests passing{a: 1}then{b: 2}results in{a: 1, b: 2})