[英]TSX file can't render imported React Component
When I import the Day component into Week component (both are.tsx files), I get the following error:当我将 Day 组件导入 Week 组件(都是 .tsx 文件)时,出现以下错误:
Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.未捕获的错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:object。
Check the render method of Week
查看
Week
的render方法
Day.tsx:日.tsx:
import React from 'react';
export default function Day () {
return (
<div>
I am day
</div>
)
}
Week.tsx周.tsx
import React from 'react';
import Day from './Day'
export default function Week () {
return (
<div>
I am Week
<Day />
</div>
)
}
If I were to remove Day from the Week component, I am able to see the Week component on the screen.如果我要从周组件中删除日,我可以在屏幕上看到周组件。 If I were to rename the Day.tsx file into Day.jsx, the issue would be fix.
如果我将 Day.tsx 文件重命名为 Day.jsx,问题就会得到解决。 It only happens with typescript. I have also tried different import conventions.
它只发生在 typescript 上。我也尝试过不同的导入约定。
Any ideas?有任何想法吗?
Add this to your tsconfig.json将此添加到您的 tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"jsx": "react-jsx"
},
"include": [
"src",
]
}
If you are using webpack also this would fix the problem如果您使用的是 webpack,这也可以解决问题
module.exports = {
//Rest of your code
resolve: {
extensions: ['.ts', '.tsx'],
},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.