Skip to content
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

useLocalStorage without 'or undefined' #2551

Open
rromikas opened this issue Apr 2, 2024 · 2 comments · May be fixed by #2556
Open

useLocalStorage without 'or undefined' #2551

rromikas opened this issue Apr 2, 2024 · 2 comments · May be fixed by #2556

Comments

@rromikas
Copy link

rromikas commented Apr 2, 2024

[isEasyToUse, setIsEasyToUse] = useLocalStorage("easyToUse", false)

Why value is boolean | undefined?

@rubickecho
Copy link

i think you will provide more infomation

@giannif
Copy link

giannif commented Apr 18, 2024

I'm surprised by this too, I expected the following to return the same types for the state and the state dispatcher

const [localStorageArray, setLocalStorageArray] = useLocalStorage<string[]>('some-array', [])
const [stateArray, setStateArray] = useState<string[]>([])

localStorageArray isstring[] | undefined
setLocalStorageArray is Dispatch<SetStateAction<string[] | undefined>>

stateArray is string[]
setStateArray is Dispatch<SetStateAction<string[]>>

I would expect this if the initialValue isn't set, but we are passing in an []

mmkal added a commit to mmkal/react-use that referenced this issue Apr 25, 2024
Fixes streamich#2551

Instead of using a single function which accepts `defaultValue?: T` - which forces a return type of `T | undefined`, use an overload. When a defaultValue is supplied, the useState-like tuple returned uses `T`. When no defaultValue is supplied, it's `T | undefined`.

Runtime usage is exaclty the same, this just improves the types.
@mmkal mmkal linked a pull request Apr 25, 2024 that will close this issue
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants