我已经建立了一个非常简单的React应用程序,并且无法将任何组件导入到index.js中。我的index.js包含主<App />
类的定义,可以正常工作,并具有以下示例:
import { IntroductionPage } from './Components/IntroductionPage.js';
有了从IntroductionPage.js导出的IntroductionPage组件的良好定义,我得到的是关于在index.js中未定义IntroductionPage的错误:
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,则一切正常。在某种程度上,我在导入/导出步骤中丢失了它。
为什么会这样呢?
请尝试仅使用以下代码从导入中删除括号:
import IntroductionPage from './Components/IntroductionPage.js';
请查看MDN上的导入文档-如果您正在使用export default
您的班级,则在像这样导入时不需要括号。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句