【发布时间】:2026-01-10 10:05:01
【问题描述】:
我已经设置了一个非常简单的 React 应用程序,但我无法将任何组件导入 index.js。我的 index.js,其中包含我的主要 <App /> 类的定义,工作正常,例如有这一行:
import { IntroductionPage } from './Components/IntroductionPage.js';
从 IntroductionPage.js 导出的 IntroductionPage 组件的定义很好,我得到的是关于 IntroductionPage 在 index.js 中未定义的错误:
React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of `App`.
in App
我不确定要更改什么——我可以看到来自 IntroductionPage.js 的 console.log 输出,所以它正在运行/编译。如果我将 IntroductionPage 组件定义移动到 index.js 中,一切都会很好。不知何故,我在导入/导出步骤中丢失了它。
为什么会发生这种情况?
【问题讨论】:
-
你能告诉我你的 IntroductionPage.js 的代码吗?您可以尝试仅使用以下内容从导入中删除括号吗?从 './Components/IntroductionPage.js' 导入 IntroductionPage;
-
我们有一个赢家....移除固定它的括号。谢谢。
-
您应该将该评论作为答案。
-
当然,现在就可以了:)
标签: reactjs webpack webpack-dev-server