I am new in typescript. I tried to solve call signature error but I couldn't figure it out.
I try to use custom hooks which it takes a context props and then return the contex like below
export const useCardContext = (): ContextProps => {
const context = useContext(CardContext);
if (!context) {
throw new Error(
`Card compound components cannot be rendered outside the Card component`
);
}
return context;
};
and here is my context props and created context
interface ContextProps {
isAccordionActive: boolean;
isModalActive: boolean;
toggleAccordion: Function;
toggleModal: Function;
}
const CardContext = createContext<ContextProps>({
isAccordionActive: false,
isModalActive: false,
toggleAccordion: () => null,
toggleModal: () => null,
});
But when I try to use useCardContext hooks inside of another function I get this error.
const data = useCardContext(); // Type 'Context<ContextProps>' has no call signatures. ts 2349
So what I did to solve this, if I export CardContext and use like below I didn't get any error.
//import usecontext and CardContext
const context = useContext(CardContext); //it is fine as expected.
My question is I would like to use the custom hooks (useCardContext) without the error. How I can solve this issue?