I am trying to write a custom hook in a react app using TypeScript that can take several, optional callback functions. However I am getting the following error:
Uncaught TypeError: Cannot read properties of undefined (reading 'onUpdate'). I write an interface like this:
interface UseMyHookProps {
onConnect:() => void;
onUpdate?:() => void;
onDisconnect?:() => {};
}
And then my hook like this:
const useMyHook = ({ onConnect = () => {}, onUpdate = () => {}, onDisconnect = () => {} }: UseMyHookProps) => {
...
// call the callback if defined.
onUpdate();
};
finally I call the hook in a component function like this:
const MyComponent = () => {
useMyHook();
...
};
I don't understand why I get this error. I say the callback is optional and provide a default argument of a void function that does nothing.