rehooks / local-storage

React hook which syncs localStorage[key] with the comp.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Default value doesn't always work

mifi opened this issue · comments

Maybe I'm expecting too much, but when I do like this:

const [user] = useLocalStorage('user', { name: 'Anakin Skywalker' });

return user.name;

...and I call this from somewhere in the code:

deleteFromStorage('user');

...I get a crash because user is now undefined and the default value is no longer being presented.

Workaround is something like this:

const [userUnsafe] = useLocalStorage('user');
const user = userUnsafe || { name: 'Anakin Skywalker' };
commented

Closing, fixed in #59