Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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 找不到命名空间';ctx';使用react-typescript创建上下文时出错_Reactjs_Typescript_Ionic4 - Fatal编程技术网

Reactjs 找不到命名空间';ctx';使用react-typescript创建上下文时出错

Reactjs 找不到命名空间';ctx';使用react-typescript创建上下文时出错,reactjs,typescript,ionic4,Reactjs,Typescript,Ionic4,我正在做一个项目,使用typescript,使用react,我很难弄清楚为什么会发生这个错误,基本上,我不能使用我在互联网上找到的任何createContext示例。 这是我从这里特别得到的:我试图使用上下文部分中显示的相同内容 import * as React from "react"; export function createCtx<A>(defaultValue: A) { type UpdateType = React.Dispatch<React.SetSta

我正在做一个项目,使用
typescript
,使用
react
,我很难弄清楚为什么会发生这个错误,基本上,我不能使用我在互联网上找到的任何
createContext
示例。 这是我从这里特别得到的:我试图使用上下文部分中显示的相同内容

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];
}
我还是不明白为什么,有人能看出我是不是做错了什么?这是我的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"
]
}

任何帮助都将不胜感激

您的文件扩展名很可能是
.ts
而不是
.tsx


因此,TypeScript正在解释
请使用
tsx
而不是
ts
它有一些细微的差异。tsx显然允许在typescript中使用jsx标记。

问题似乎出在配置文件中。我能够在typescript平台上编译它。我不能在这里添加链接,因为它太长了。你的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"
]
}