您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Typescript中反应createContext问题?

在Typescript中反应createContext问题?

看来的defaultValueReact.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>
  );
}
其他 2022/1/1 18:16:09 有426人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶