imbhargav5 / rooks

Essential React custom hooks ⚓ to super charge your components!
https://rooks.vercel.app
MIT License
3.19k stars 216 forks source link

useLocalstorageState hook stores strings with extra double quotes #1728

Closed tayurus closed 11 months ago

tayurus commented 1 year ago

I have created codesandbox playground for this issue https://codesandbox.io/s/cold-sun-tm82rc?file=/App.js:0-674

You can check console of the playground image

nalladev commented 1 year ago

values are stored as strings in localStorage you have to parse the value recieved from localStorage using JSON.parse function eg: console.log( "my-app:count with localStorage.getItem = ", JSON.parse(localStorage.getItem("my-app:count")) );

I have created codesandbox playground for this issue https://codesandbox.io/s/cold-sun-tm82rc?file=/App.js:0-674

tayurus commented 1 year ago

values are stored as strings in localStorage you have to parse the value recieved from localStorage using JSON.parse function eg: console.log( "my-app:count with localStorage.getItem = ", JSON.parse(localStorage.getItem("my-app:count")) );

I have created codesandbox playground for this issue https://codesandbox.io/s/cold-sun-tm82rc?file=/App.js:0-674

It seems not obvious. At least it needs to be described in docs - https://rooks.vercel.app/docs/useLocalstorageState

nalladev commented 1 year ago

there is no need for directly taking the data from local Storage. useLocalstorageState hook will do the parsing for you automatically if you are using the state given by the hook the whole point of the hook is to use the state and and reduce your work. even though if you have an exeptional case you can still get the data by using JSON parsing that is the normal behaviour for localStorage access

nalladev commented 1 year ago

A similar issue was raised in https://github.com/streamich/react-use/issues/2224 see the reply

nalladev commented 1 year ago

That's the normal behaviour, as described here

stale[bot] commented 11 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.