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

Error when new value is not JSON #16

Open
deevus opened this issue Aug 6, 2019 · 13 comments
Open

Error when new value is not JSON #16

deevus opened this issue Aug 6, 2019 · 13 comments

Comments

@deevus
Copy link

deevus commented Aug 6, 2019

SyntaxError: Unexpected token e in JSON at position 1
  at JSON.parse(<anonymous>)
  at current(./node_modules/use-persisted-state/dist/use-persisted-state.m.js:1:766)
  at n(./node_modules/@use-it/event-listener/dist/event-listener.m.js:1:199)
  at n(./node_modules/@sentry/browser/esm/helpers.js:70:1)

See line 12 below:

useEventListener('storage', ({ key: k, newValue }) => {
const newState = JSON.parse(newValue);
if (k === key && state !== newState) {
setState(newState);
}
});

Other modules may use localStorage and not store JSON.

@deevus
Copy link
Author

deevus commented Aug 6, 2019

It might be simple enough to short circuit when k !== key

@ricardo-devis-agullo
Copy link

Hi, I see this got fixed.

Could you publish to NPM a new version for this fix? :)

@timuric
Copy link

timuric commented Aug 21, 2019

Would very much appreciate this fix! :)

@szymn
Copy link

szymn commented Sep 3, 2019

@donavon any chance you could publish a new version with this fix to NPM?

@michu2k
Copy link

michu2k commented Oct 3, 2019

Hi @donavon, so can you publish to NPM a new version with this fix :) ?

@cammanderson
Copy link

Hi All, Just +1'ing this for a fix as well. Not sure if we want to make it safer to use try {} catch for added safety also?

@DatChoob
Copy link

DatChoob commented Jan 7, 2020

@donavon Would really appreciate this fix

@smerth
Copy link

smerth commented Jan 21, 2020

@donavon, +1 - looking forward to the NPM version with this fix. Thanks for the great hook

@williamgrosset
Copy link

@donavon +1! :)

@deevus
Copy link
Author

deevus commented Jan 27, 2020

@Luke-Markham
Copy link

Luke-Markham commented Feb 5, 2020

1+ broke my app because of this haha

@deevus
Copy link
Author

deevus commented Feb 20, 2020

It looks like some nice person has published version 0.3.1 here https://www.npmjs.com/package/@smartrent/use-persisted-state

If you don't want to change your import references to @smartrent/use-persisted-state and you use yarn, you can add it as an alias using the following command:

yarn add use-persisted-state@npm:@smartrent/use-persisted-state

@astoilkov
Copy link

I see this module is not well maintained anymore. You could check out the module I built https://github.com/astoilkov/use-local-storage-state. The good thing is we used it in our production app so maintenance should be solid.

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