Repository navigation
Replies: 1 comment
|
An opt-in like Today I’d probably solve it in userland like this: const [id, setId] = useLocalStorage<string | null>('bank-id', null)
useEffect(() => {
if (id == null) {
setId(crypto.randomUUID())
}
}, [id, setId])If the hook grows an option, I’d keep it:
Sketch: const initial = initialValue instanceof Function ? initialValue() : initialValue
if (options.persistInitialValue && window.localStorage.getItem(key) == null) {
window.localStorage.setItem(key, serializer(initial))
}That preserves the current behavior while covering the “generate once, then persist forever” use case. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
We'd like to use the storage hooks to handle random values, e.g. a generated UUID.
However the hooks do not persist the initial value. Hence reloading the hook results in a new value.
Suggestion is to add an option, .e.g
persistInitialValue, defaulting to false, which does this.Happy to contribute some code if you think this is a good idea.
All reactions