fix(vanilla/utils/atomWithStorage): apply reviver when parsing subscription updates#3326
Merged
dai-shi merged 3 commits intoJun 11, 2026
Merged
Conversation
…iption updates in createJSONStorage
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
commit: |
dai-shi
approved these changes
Jun 8, 2026
dai-shi
left a comment
Member
There was a problem hiding this comment.
Looks great! Thanks for your contribution.
I'll merge this when I prepare a patch release.
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.
Bug
When a custom
reviveris passed tocreateJSONStorage, it is correctly applied ingetItem(viaJSON.parse(str, options?.reviver)), but not in the internalcreateHandleSubscribehandler that fires when the underlying string storage emits a change (e.g. via a cross-tabstorageevent or a customsubscribecallback).This means that if an atom value is a class instance or otherwise requires revival from the raw JSON object graph, any subscription-driven update receives a plain object instead of the revived value — causing runtime errors for callers that invoke methods on the result.
Reported in discussion #3101.
Root cause
createHandleSubscribecalledJSON.parse(v || '')without forwardingoptions?.reviver:Fix
One-line change in
src/vanilla/utils/atomWithStorage.ts: passoptions?.reviveras the second argument toJSON.parseinsidecreateHandleSubscribe.Test
A new test
createJSONStorage reviver is applied on subscription updates (#3101)is added totests/react/vanilla-utils/atomWithStorage.test.tsx. It:createJSONStoragewith a reviver that turns plain{ n: number }objects intoItemclass instances.value.doubled()— a method that only exists onItem.CustomEvent.value instanceof Itemisfalse, and the component rendersNOT_ITEM. With the fix it renders the correct doubled value.Closes #3101