New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useLocalStorageHook set defaultValue to undefined but get empty string #2642
Comments
@rtivital what are your thoughts on this? I can create a PR if needed. |
I'm fine with this, feel free to create a PR |
I have a question about how to implement this. I don't like setting the return type to An alternative I considered was to add a second generic type parameter: What do you think? @rtivital |
User should specify value type on their side: const [colorScheme, setColorScheme] = useLocalStorage<ColorScheme | undefined>({
key: 'color-scheme',
defaultValue: undefined,
}); |
I see. This does leave the possibility of misuse by the user, |
Why? As I see it, the user defines the type and gets exactly what is defined. |
If the user forgets to add a default value and calls |
That is an error on user side, on library level we should not assume that the user will make errors in their code. It will also make harder for the users who did provide default value. For example, in this case |
In this example, if the return type would be of type I think this accurately models the situation, and I don't think the type would include unneeded |
I created #2872. |
What package has an issue
@mantine/hooks
Describe the bug
Hello, why choose Nullish coalescing operator to make defaultValue which is undefined or null to empty string in useLocalStorageHook?
It's a little weird if I expect an undefined/null defaultValue but get an empty string.
Also, is there a typo here in use-local-storage?
If set to true, value will be update in useEffect after mount
What version of @mantine/hooks page do you have in package.json?
latest
If possible, please include a link to a codesandbox with the reproduced problem
https://codesandbox.io/s/kind-star-v25pry?file=/src/App.tsx
Do you know how to fix the issue
Yes
Are you willing to participate in fixing this issue and create a pull request with the fix
Yes
Possible fix
The text was updated successfully, but these errors were encountered: