我工作的一个项目,react使用typescript,我有一个坏的时间弄清楚为什么这个错误发生的事情,我基本上不能使用任何createContext的例子,我在互联网上找到,因为这一点。我具体是从这里获得的:https : //github.com/typescript-cheatsheets/react-typescript-cheatsheet我正在尝试使用“上下文”部分中显示的内容。
import * as React from "react";
export function createCtx<A>(defaultValue: A) {
type UpdateType = React.Dispatch<React.SetStateAction<typeof defaultValue>>;
const defaultUpdate: UpdateType = () => defaultValue;
const ctx = React.createContext({
state: defaultValue,
update: defaultUpdate
});
function Provider(props: React.PropsWithChildren<{}>) {
const [state, update] = React.useState(defaultValue);
return <ctx.Provider value={{ state, update }} {...props} />;
}
return [ctx, Provider] as [typeof ctx, typeof Provider];
}
Run Code Online (Sandbox Code Playgroud)
问题是,每次它说有此错误时,都无法ctx在行中找到名称空间:
return <ctx.Provider value={{ state, update }} {...props} …Run Code Online (Sandbox Code Playgroud)