Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils/atomWithStorage): Prevent createJSONStorage from adding subscribe method when window.addEventListener is not defined #1375

Merged
merged 2 commits into from
Aug 30, 2022

Conversation

tekkeon
Copy link
Contributor

@tekkeon tekkeon commented Aug 29, 2022

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 that window.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 set addEventListener as undefined, I had to cast window as any, which requires those parentheses. Alternatively, I could do something like the following if preferred:

const windowAny = window as any
windowAny.addEventListener = undefined

Check List

  • yarn run prettier for formatting code and docs

Sorry, something went wrong.

…scribe method when window.addEventListener is not defined

Added a condition to the if statement that already existed that, in addition to checking whether the global window object exists, also checks that window.addEventListener exists.
@vercel
Copy link

vercel bot commented Aug 29, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
jotai ✅ Ready (Inspect) Visit Preview Aug 29, 2022 at 5:39AM (UTC)

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 5869c62:

Sandbox Source
React Configuration
React Typescript Configuration
React Browserify Configuration
React Snowpack Configuration
Next.js with custom Babel config Configuration
React with custom Babel config Configuration

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!
Don't worry about prettier putting ; in front. It's how it works.

@dai-shi
Copy link
Member

dai-shi commented Aug 29, 2022

the value in [value] = useAtom(atom) shows as unknown, it doesn't throw an error now.

Maybe it was using old TS? Can't tell for sure without a reproduction.

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.

React Native (Expo) atomWithStorage error: "window.addEventListener is not a function"
2 participants