I was looking into using this to replace some custom storage access hooks in a somewhat large codebase and one issue showed up that was pretty surprising. This hook writes the given default value into storage if nothing is found for the given key. This is undesirable behavior from my perspective and I'd like to opt out of it. I'm working around it for the time being by using my own wrapper hook around this one like so:
function useStorage<T>(key: string, defaultValue: T | null = null) {
const [value, setValue, clearValue] = useStorageState(key);
return useMemo(() => {
if (value === undefined) {
return [defaultValue, setValue, clearValue];
}
return [value, setValue, clearValue];
}, [defaultValue, value, setValue, clearValue]);
}
What do you think about adding an option I could pass to useStorageState to opt-out of having defaults written into localStorage and filling up lots of keys with unnecessary values?
I was looking into using this to replace some custom storage access hooks in a somewhat large codebase and one issue showed up that was pretty surprising. This hook writes the given default value into storage if nothing is found for the given key. This is undesirable behavior from my perspective and I'd like to opt out of it. I'm working around it for the time being by using my own wrapper hook around this one like so:
What do you think about adding an option I could pass to
useStorageStateto opt-out of having defaults written into localStorage and filling up lots of keys with unnecessary values?