astoilkov / use-local-storage-state

React hook that persists data in localStorage
MIT License
1.17k stars 41 forks source link

Hooks sets localStorage value to string 'undefined' rather than doing nothing. #30

Closed ozyman42 closed 3 years ago

ozyman42 commented 3 years ago
const [storedData, setStoredData, { removeItem }] = useLocalStorageState("testing");
const value = localStorage.getItem('testing')
console.log(typeof value);
console.log(value)

this outputs

string
undefined

this should ideally output

object
null

Why is the hook updating local storage when the client code never instructs it to do so?

astoilkov commented 3 years ago

This is caused by the change that was made when resolving https://github.com/astoilkov/use-local-storage-state/issues/26.

Probably I shouldn't write the value on the initial render when a value isn't passed.

astoilkov commented 3 years ago

I just made a new release that includes the change. Thanks for reporting!