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

[useLocalStorage] #2224

Closed
rikusen0335 opened this issue Nov 13, 2021 · 2 comments
Closed

[useLocalStorage] #2224

rikusen0335 opened this issue Nov 13, 2021 · 2 comments

Comments

@rikusen0335
Copy link

rikusen0335 commented Nov 13, 2021

What is the current behavior?
useLocalStorage sets string in string: Something like:

const [v, setv] = useLocalStorage("theme");

<button onClick={() => setv("black")}>set</button>

will set "black" to local storage, NOT black

As I expected, localStorage.setItem("theme", "black") will set black

They means localStorage.getItem("theme") === '"dark"' will return true but localStorage.getItem("theme") === 'dark' will return false.

Steps to reproduce it and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have extra dependencies other than react-use. Paste the link to your JSFiddle or CodeSandbox example below:
https://codesandbox.io/s/react-fw-react-use-localstorage-vpu4i

What is the expected behavior?

A little about versions:

  • OS: Windows 10 (But remotely connect to and develop on Fedora 34)
  • Browser (vendor and version): Chromiun Edge 95.0.1020.44 (64bit)
  • React: 17.0.2
  • react-use: 17.3.1
  • Did this worked in the previous package version? I think not, I haven't used this package yet.
@flaval
Copy link

flaval commented Dec 4, 2021

That's the normal behaviour, as described here : https://github.com/streamich/react-use/blob/master/docs/useLocalStorage.md, data is serialized / deserialized before beeing sent / get back to / from local storage, and the default serializer and deserializer are JSON.stringify and JSON.parse. You can override serializer to not tranform data to JSON if you don't want tranform your data to string : https://codesandbox.io/s/react-fw-react-use-localstorage-forked-in97s?file=/src/App.tsx

@rikusen0335
Copy link
Author

I understand, thanks!

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