I am trying to set up a React store using useReducer and useContext hooks. The React.createContext(defaultValue) is creating issues with my TS checker. I have tried a few different things, but essentially, I createContext(null) then in the component useReducer() to set state and dispatch, but when I call the Provider and pass the value as {state, dispatch}, it doesn't tells me "Type '{ state: any; dispatch: React.Dispatch; }' is not assignable to type 'null'.
I don't understand this because by the time it errors, I have assigned state and dispatch and the value should no longer be null.
Here is the Context Provider wrapper that I am trying to create.
import React, { createContext, useReducer, FC, Dispatch } from 'react';
import storeReducer, { initialState } from './reducer';
import { Action, State } from './types';
import { isNull } from 'util';
const StoreContext = createContext(null);
const StoreProvider:FC = ({ children }) => {
const [state, dispatch] = useReducer(storeReducer, initialState);
const value = {state, dispatch}
return (
<StoreContext.Provider value={value}>
{children}
</StoreContext.Provider>
);
};
export { StoreProvider, StoreContext };
export interface IStoreContext {
dispatch: (action: Action<any>) => {};
state: State;
}
if I leave it as simply const StoreContext = createContext();, then it complains that defaultValue is not being defined.
The crazy thing is I have lifted this from an old project and had no issues compiling.