看来的defaultValue
值React.createContext
应为以下类型:
interface IContextProps {
state: IState;
dispatch: ({type}:{type:string}) => void;
}
一旦Context
为此类型创建了对象,例如:
export const AdminStore = React.createContext({} as IContextProps);
提供者React组件应该不再抱怨该错误。
以下是更改列表:
import React, { useReducer } from "react";
import { initialState, IState, reducer } from "./reducer";
interface IContextProps {
state: IState;
dispatch: ({type}:{type:string}) => void;
}
export const AdminStore = React.createContext({} as IContextProps);
export function AdminStoreProvider(props: any) {
const [state, dispatch] = useReducer(reducer, initialState);
const value = { state, dispatch };
return (
<AdminStore.Provider value={value}>{props.children}</AdminStore.Provider>
);
}