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

Works across tabs, but not within a single tab #3

Open
raiskila opened this issue Feb 16, 2019 · 3 comments
Open

Works across tabs, but not within a single tab #3

raiskila opened this issue Feb 16, 2019 · 3 comments

Comments

@raiskila
Copy link
Contributor

raiskila commented Feb 16, 2019

Hi,

While the storage event is fired when another page modifies local storage, it's not fired when the current page modifies local storage. Thus two components on the same page using this hook won't stay in sync. Demo codesandbox

The setState function should probably also be wrapped in useCallback to prevent its reference identity from changing on every render. This is consistent with the setter returned by useState and the dispatch function returned by useReducer, and allows for things like passing the function down in React context without causing components downstream to update constantly.

@nwalters512
Copy link
Member

Yeah, it's unfortunate the localStorage API works that way. Do you have any thoughts on how to account for that? We could probably fire our own event within the window.

Good suggestion regarding useCallback, would definitely accept a PR with that implemented.

@raiskila
Copy link
Contributor Author

I opened a PR about the useCallback change.

I used a CustomEvent to solve the event issue in a version of this hook I wrote some time ago, but unfortunately CustomEvent requires a polyfill in IE. I'm not sure what would be the most "clean" way to do this cross-browser.

@nwalters512
Copy link
Member

I'm not super concerned about IE - I'd be fine saying "you have to polyfill if you want to support IE". Could even include it by default, though I'm not thrilled about that. We could also just do things "the old-fashioned way".

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

No branches or pull requests

2 participants