So I'm having a very weird issue with React Context + Typescript.
In the above example, you can see what I'm trying to do actually work. Essentially I'm managing state with the new useContext method, and it works perfectly.
However, when I try to do this on my box, it cannot seem to find the state values being passed through the useReducer.
export function AdminStoreProvider(props: any) {
const [state, dispatch] = useReducer(reducer, initialState);
// state.isAuth is avail here
// state.user is avail here
const value = { state, dispatch };
// value.state.isAuth is avail here
return (
/* value does not contain state once applied to the value prop */
<AdminStore.Provider value={value}>{props.children}
</AdminStore.Provider>
);
}
Error message:
Type '{ state: { isAuth: boolean; user: string; }; dispatch:
Dispatch<Actions>; }' is missing the following properties from type
'IState': isAuth, user
Keep in mind the code I'm using is exactly what I'm using on my box, I've even downloaded the code from sandbox and tried running it, and it doesn't work.
I'm using VSCode 1.31
I've managed to deduce that if I change how I create my context from:
export const AdminStore = React.createContext(initialState);
to
export const AdminStore = React.createContext(null);
The value property no longer throws that error.
However, now useContext returns an error: state doesn't exist on null. And same if I set defaultState for context to {}.
And of course if I
React.createContext();
Then TS yells about no defaultValue being provided.
In sandbox, all 3 versions of creating the context object work fine.
Thanks in advance for any advice.
Type '{ state: { isAuth: boolean; user: string; }; dispatch: Dispatch<Actions>; }' is not assignable to type 'IContextProps'. Types of property 'dispatch' are incompatible. Type 'Dispatch<Actions>' is not assignable to type '({ type }: { type: string; }) => void'. Types of parameters 'value' and '__0' are incompatible. Type '{ type: string; }' is not assignable to type 'Actions'. Property 'value' is missing in type '{ type: string; }' but required in type 'ILogout'
– Jenette