我试图创建一个context来保存state和reducer,但是它在return语句上给了我错误信息,我不知道该怎么做:
运算符“〈”不能应用于类型“boolean”和“RegEx”。ts(2365)
未终止的正则表达式文本.ts(1161)
import { createContext, FunctionComponent, PropsWithChildren, ReactNode, useReducer } from "react";
import reducer from "./reducer";
// stored data
export type storeType = {
message: string | null
};
const initialState: storeType = {
message: null
}
const AppContext = createContext<storeType>(initialState);
const CxtProvider = ({ children }: PropsWithChildren) => {
const [currentState, dispatch] = useReducer(reducer, initialState);
return <AppContext.Provider value={{
currentState,
dispatch
}}>{children}</AppContext.Provider>
};
export default CxtProvider;
有人知道我做错了什么吗?谢谢
编辑1:谢谢你的想法,我试着传播对象,但没有帮助,现在甚至有更多的错误。
Compiled with problems:
ERROR in src/utils/context/context.ts
TS1161: Unterminated regular expression literal.
ERROR in src/utils/context/context.ts:19:13
TS2503: Cannot find namespace 'AppContext'.
ERROR in src/utils/context/context.ts:19:33
TS1005: '>' expected.
ERROR in src/utils/context/context.ts:19:33
TS2304: Cannot find name 'value'.
ERROR in src/utils/context/context.ts:19:38
TS1005: ';' expected.
ERROR in src/utils/context/context.ts:20:9
TS1128: Declaration or statement expected.
ERROR in src/utils/context/context.ts:20:12
TS2304: Cannot find name 'currentState'.
ERROR in src/utils/context/context.ts:20:12
TS2695: Left side of comma operator is unused and has no side effects.
ERROR in src/utils/context/context.ts:21:9
TS2304: Cannot find name 'dispatch'.
ERROR in src/utils/context/context.ts:22:6
TS1128: Declaration or statement expected.
ERROR in src/utils/context/context.ts:22:7
TS1128: Declaration or statement expected.
ERROR in src/utils/context/context.ts:22:8
TS1109: Expression expected.
ERROR in src/utils/context/context.ts:22:8
TS2365: Operator '<' cannot be applied to types 'boolean' and 'RegExp'.
ERROR in src/utils/context/context.ts:22:10
TS18004: No value exists in scope for the shorthand property 'children'. Either declare one or provide an initializer.
ERROR in src/utils/context/context.ts:23:1
TS1128: Declaration or statement expected.
2条答案
按热度按时间fruv7luv1#
您应该展开
currentState
值:u2nhd7ah2#
最后,它解决了转移到不同的文件的部分:上下文,还原器,提供器。不知道为什么。
谢谢你的建议
我想问题出在createContext类型中的某个地方,它没有reducer成员