Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在React Typescript中使用上下文API提供useReducer?_Reactjs_Typescript_React Typescript_Context Api_Use Reducer - Fatal编程技术网

Reactjs 如何在React Typescript中使用上下文API提供useReducer?

Reactjs 如何在React Typescript中使用上下文API提供useReducer?,reactjs,typescript,react-typescript,context-api,use-reducer,Reactjs,Typescript,React Typescript,Context Api,Use Reducer,我想全局提供useReducer,但createContext()要求提供默认值,如果我指定null,则会抛出错误。我尝试分配空对象而不是空对象。但是如果我这样做,我就无法访问我的userReducer import React, { createContext, useContext, useReducer, Dispatch } from "react"; import { reducer, initialState } from "./reducer"

我想全局提供useReducer,但createContext()要求提供默认值,如果我指定null,则会抛出错误。我尝试分配空对象而不是空对象。但是如果我这样做,我就无法访问我的userReducer

import React, { createContext, useContext, useReducer, Dispatch } from "react";
import { reducer, initialState } from "./reducer";

const AppContext = createContext(null)

export const useAppContext = () => useContext(AppContext)

export const AppContextProvider: React.FC = ({children}) => (
    <AppContext.Provider value={useReducer(reducer, initialState)}>
        {children}
    </AppContext.Provider>
)
import React,{createContext,useContext,useReducer,Dispatch}来自“React”;
从“/reducer”导入{reducer,initialState};
const AppContext=createContext(null)
导出常量useAppContext=()=>useContext(AppContext)
导出常量AppContextProvider:React.FC=({children})=>(
{儿童}
)

如何解决此问题?

类型在哪里?您没有键入任何内容,只是在问如何键入useReducer?有很多类似的问题你也应该键入你的上下文
createContext(null)
我是ts新手。我可以不用ts来做。但是我想知道如何用ts来做。我想要一个线索!你写它的方式让我很困惑:)基本上你是在问如何做
value={array}
,因为
useReducer
返回一个数组。如果您可以编写
const[state,dispatch]=useReducer(…)
,它将帮助您找出缺少的类型。一个附带问题是
Context
+
useReducer
本质上是
redux
?(不是redux的粉丝)。我发现上面的代码很好地替代了
redux
。如果你碰巧有答案,谢谢你。