E
E
Elena2022-01-20 14:31:34
typescript
Elena, 2022-01-20 14:31:34

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: ''
  }
}
)

how do i pass an object without specifying specific keys?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2022-01-20
@Elena0394

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 question

Ask a Question

731 491 924 answers to any question