In a basic React custom hook in which the only difference is returning with [ ]
or { }
:
const useCustomHookSquare= () => {
const [state, setState] = useState<Type>(initialState);
return [state, storeBounceBox];
}
const useCustomHookCurly= () => {
const [state, setState] = useState<Type>(initialState);
return {state, storeBounceBox};
}
This works:
const {state, setState} = useCustomHookCurly();
// or even
const {setState} = useCustomHookCurly();
But doing the exact same thing deconstructing with square brackets gives me a TypeScript error when calling the setState function (or any other function exported this way):
const [state, setState] = useCustomHookSquare();
setState(someState); // "This expression is not callable. Type has no call signatures".
I feel like there is some basic JavaScript concept I'm missing here. Why does the {}
version allow me to call the function but the []
version doesn't?