use-persisted-state
use-persisted-state copied to clipboard
Error when new value is not JSON
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.
It might be simple enough to short circuit when k !== key
Would very much appreciate this fix! :)
@donavon any chance you could publish a new version with this fix to NPM?
Hi @donavon, so can you publish to NPM a new version with this fix :) ?
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?
@donavon Would really appreciate this fix
@donavon, +1 - looking forward to the NPM version with this fix. Thanks for the great hook
@donavon +1! :)
I have tweeted @donavon here https://twitter.com/deevus/status/1221934141488283649
1+ broke my app because of this haha
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
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.