fix(utils/atomWithStorage): Prevent createJSONStorage from adding subscribe method when window.addEventListener is not defined #1375
+40
−1
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.
Related Issues
Fixes #1373
Summary
Added a condition to the if statement that already existed that, in addition to checking whether the global
window
object exists, also checks thatwindow.addEventListener
exists. Also added a simple test to prevent regression in the future.I tested this in the project I first discovered the bug using yalc (since React Native doesn't support symlinks), and, other than a weird type issue where the
value
in[value] = useAtom(atom)
shows as unknown, it doesn't throw an error now. I doubt my one-line change could have actually messed with the typings, but wanted to call it out for you to verify as the expert.Note: The beforeAll function in the added test looks a bit odd with the
;
in front but was added by prettier, probably to prevent the parentheses around(window as any)
from accidentally being used to invoke a function. In order to setaddEventListener
asundefined
, I had to castwindow
asany
, which requires those parentheses. Alternatively, I could do something like the following if preferred:Check List
yarn run prettier
for formatting code and docs