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

Unexpected token c in JSON at position 0 #31

Open
burtonator opened this issue May 10, 2020 · 7 comments
Open

Unexpected token c in JSON at position 0 #31

burtonator opened this issue May 10, 2020 · 7 comments

Comments

@burtonator
Copy link

I'll try to post more but I'm getting this when values are loaded between windows

Uncaught SyntaxError: Unexpected token c in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.current (:8500/Users/burton/projects/polar-app/packages/polar-bookshelf/node_modules/use-persisted-state/dist/use-persisted-state.js:1)
    at e (:8500/Users/burton/projects/polar-app/packages/polar-bookshelf/node_modules/@use-it/event-listener/dist/event-listener.js:1)
@joernroeder
Copy link

ran into the same issue as the old localstorage state was not json.parsable
https://github.com/donavon/use-persisted-state/blob/develop/src/createStorage.js#L9

i am using https://github.com/astoilkov/use-local-storage-state for now as in cases where the parsing fails the default value is returned. https://github.com/astoilkov/use-local-storage-state/blob/master/index.ts#L15

@rebeccakeller
Copy link

code in use-local-storage-state is prone to same issue. Although it checks for null a newValue that fails to JSON.parse will still explode

@joernroeder
Copy link

@rebeccakeller it solved my issue as it catches the json.parse explosion and returns the defaultValue

@scscgit
Copy link

scscgit commented Jun 23, 2020

I also stumbled upon this, and I believe this is a really severe issue, as any app in the production can completely break down, making the user unable to access it at all if they had previously accidentally stored a variable in the browser using the native JS localStorage, and there's no way for developers to even notice this hazard during the development, for example if they switch the libraries and aren't lucky enough to keep the old state. Could you please mention this severe limitation in the Readme, so that people don't mistakenly use this library as long as this issue is known? I've been directed to this project from somewhere, but it seems the library https://github.com/astoilkov/use-local-storage-state is generally a better choice for any similar purpose. Maybe the projects could be even merged to serve as a universal solution if there's no plan to provide anything custom.

@abidRahim
Copy link

@donavon is there a PR raised for this issue, so that someone could contribute?

This is a production level bug

@Rendez
Copy link

Rendez commented Nov 11, 2020

I just opened a PR to astoilkov/use-local-storage-state#14 to allow using sessionStorage too. Once that's merged, I believe it's a much better alternative, as it also handles other cases nicely, like events - unrelated keys or non-matching Storage events.

@astoilkov
Copy link

use-local-storage-state author here. I can confirm that this issue doesn't exist in my implementation. I also have been actively maintaining it for the past 7 months and will be happy to see new people using it.

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

7 participants