rehooks / local-storage

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

handle disabled localstorage (optionally?)

gerbyzation opened this issue · comments

At the moment when the localStorage api is disabled in the browser an error is raised that is not handled by the hook.

The option to gracefully degrade and return the default value would be useful in this scenario. Is this something you'd consider? If so would be happy to help.

commented

Sounds good! We can return the state and it's setter.

This approach for detecting if it's available looks good:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Feature-detecting_localStorage

One minor caveat though, we shouldn't use the key "test" since we are not an end user and can't assume they won't use that (probably in their own tests). Can just use a string like "@rehooks/localstorage:" + new Date().toISOString() instead.

I would suggest making this mandatory instead of optional, at least for now.

@jharrilim I've opened a PR #61. When you have time would love to get your feedback on it!