react-use icon indicating copy to clipboard operation
react-use copied to clipboard

useLocalStorage without 'or undefined'

Open rromikas opened this issue 10 months ago • 2 comments

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

Why value is boolean | undefined?

rromikas avatar Apr 02 '24 17:04 rromikas

i think you will provide more infomation

rubickecho avatar Apr 11 '24 10:04 rubickecho

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 []

giannif avatar Apr 18 '24 14:04 giannif