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

local-state-key-change example has a syntax error if there's a name in local storage already #217

Closed
jaharnum opened this issue Jun 23, 2023 · 1 comment

Comments

@jaharnum
Copy link
Contributor

If I open http://localhost:3000/isolated/examples/local-state-key-change.js with a name already stored in local storage state from testing exercise 2, the page will not load and gives a few error messages.

Uncaught SyntaxError: Unexpected token 'e', "test" is not valid JSON
    at parse (<anonymous>)
    at utils.js:18:1
    at mountState (react-dom.development.js:16986:1)
    at Object.useState (react-dom.development.js:17699:1)
    at Object.useState (react.development.js:1622:1)
    at useLocalStorageState (utils.js:15:1)
    at Greeting (local-state-key-change.js:10:1)
    at renderWithHooks (react-dom.development.js:16305:1)
    at mountIndeterminateComponent (react-dom.development.js:20074:1)
    at beginWork (react-dom.development.js:21587:1)
The above error occurred in the <Greeting> component:

    at Greeting (http://localhost:3000/static/js/src_examples_local-state-key-change_js.chunk.js:32:3)

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.

It works fine when I remove the name beforehand.

I am guessing this is due to the fact that I haven't completed the extra credit that turns the input name into JSON before storing it, but the useLocalStorageState in utils expects it to be in that format.

This is handled in 02.extra-4.js with a try/catch that simply removes the offending item, but not in utils.js, so it should be an easy fix :)

@jaharnum jaharnum changed the title local-state-key-change example has a JSON decoding error if there's a name in local storage already local-state-key-change example has a syntax error if there's a name in local storage already Jun 23, 2023
@kentcdodds
Copy link
Member

Thanks for this! Would you be willing to make a pull request for this fix?

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