On my app I have this:
const restaurantsArr = [{ name: McDonald, rating: 5 }]
const RestaurantContext = createContext();
const [restaurants, setRestaurants] = useState(restaurantsArr);
For Provider, I want to pass my destructured variables from useState.
<Provider value={[restaurants, setRestaurants]} />
TS yelling at me, that createContext must have value, but if my createContext has empty array for example I have errors.
How I should create interface ContextProps?
In fact, I have array which contains another array of objects and a function.
I'm fighting with this for a couple of hours and I have warnings and errors all the time...
Ok, so I have this now:
export const RestaurantsContext = createContext<any>([]);
interface Props {
children: ReactElement;
}
const RestaurantsProvider: React.FC<Props> = ({children}) => {
const [restaurants, setRestaurants] = useState(restaurantsArr)
return (
<RestaurantsContext.Provider value={[restaurants, setRestaurants]}>
{children}
</RestaurantsContext.Provider>
)
But It's just walk-around with any on export const RestaurantsContext = createContext<any>([]).
What I want to do is to have something like:
interface ContextProps {
//and something here
}
But how should I type [data, setData] ? It's an array with array of objects and with setState function.