Hi :) I'm running into an issue when trying to set up context with createContext. I need/want to access state and dispatch in my child components.
I'd also like to know: is the way I'm trying to pass down state and dispatch the right way? Or is there another (better) way to do this?
The error I get: No value exists in scope for the shorthand property 'state'. Either declare one or provide an initializer.
Here's (part of) my App.tsx:
interface IState {
hasStarted: boolean;
chosenAmount: string;
chosenCategory: string;
chosenDifficulty: string;
}
const initialState: IState = {
hasStarted: false,
chosenAmount: "",
chosenCategory: "",
chosenDifficulty: "",
};
// ...
interface IStateContext {
state: IState
dispatch: React.Dispatch<Action>
}
export const StateContext = createContext<IStateContext>({state, dispatch}); // error here
export const App: React.FC = () => {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<StateContext.Provider value={{state, dispatch}}>
{state.hasStarted ? <QuestionsPage /> : <StartPage dispatch={dispatch} />}
</StateContext.Provider>
);
};
export const StateContext = createContext();