use-persisted-state icon indicating copy to clipboard operation
use-persisted-state copied to clipboard

Error when new value is not JSON

Open deevus opened this issue 6 years ago • 13 comments

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:

https://github.com/donavon/use-persisted-state/blob/02a847a8a46360cac1ee8f3c5ad3726875667c1e/src/usePersistedState.js#L11-L16

Other modules may use localStorage and not store JSON.

deevus avatar Aug 06 '19 06:08 deevus

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

deevus avatar Aug 06 '19 06:08 deevus

Hi, I see this got fixed.

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

ricardo-devis-agullo avatar Aug 19 '19 16:08 ricardo-devis-agullo

Would very much appreciate this fix! :)

timuric avatar Aug 21 '19 11:08 timuric

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

szymn avatar Sep 03 '19 08:09 szymn

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

michu2k avatar Oct 03 '19 09:10 michu2k

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?

cammanderson avatar Oct 16 '19 05:10 cammanderson

@donavon Would really appreciate this fix

DatChoob avatar Jan 07 '20 19:01 DatChoob

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

smerth avatar Jan 21 '20 21:01 smerth

@donavon +1! :)

williamgrosset avatar Jan 21 '20 22:01 williamgrosset

I have tweeted @donavon here https://twitter.com/deevus/status/1221934141488283649

deevus avatar Jan 27 '20 23:01 deevus

1+ broke my app because of this haha

Luke-Markham avatar Feb 05 '20 08:02 Luke-Markham

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

deevus avatar Feb 20 '20 04:02 deevus

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.

astoilkov avatar Mar 14 '20 20:03 astoilkov