Answer the question
In order to leave comments, you need to log in
How to pass type any to createContext?
I have a context React.createContext()
I get an object from it. The TS wants me to pass the following object to the context:
React.createContext(
{
name: '',
link: '',
owner: {
_id: ''
}
}
)
Answer the question
In order to leave comments, you need to log in
it is better to define the type explicitly, and when creating the context, insert an empty stub - all the same, the real value will be substituted through the provider.
type MyContextType = {
name: string;
link: string;
owner: {
_id: string;
};
}
const context = React.createContext({} as MyContextType);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question