Vic*_*lho 7 typescript reactjs ionic4
我工作的一个项目,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)
我仍然无法弄清楚为什么,有人可以看看我在这里做错了什么吗?这是我的tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"noImplicitAny": false,
"strictNullChecks": false
},
"include": [
"src"
]
}
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
luk*_*ter 15
您的文件扩展名很可能.ts不是.tsx。
因此,TypeScript被解释<ctx.Provider为强制转换并尝试Provider在名称空间中查找类型ctx。