繁体   English   中英

在 React 中导入模块时出错

[英]Errors while importing module in React

在我导入Imex之前,该应用程序运行良好,没有任何错误,它显示 4 个错误,如下所示

1. Warning: React.jsx: 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, or you might have mixed up default and named imports.
Check your code at App.js:8 at App

2. Uncaught Error: Element 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, or you might have mixed up default and named imports.

Check the render method of `App`.
    at createFiberFromTypeAndProps (react-dom.development.js:25058)
    at createFiberFromElement (react-dom.development.js:25086)
    at createChild (react-dom.development.js:13446)
    at reconcileChildrenArray (react-dom.development.js:13719)
    at reconcileChildFibers (react-dom.development.js:14125)
    at reconcileChildren (react-dom.development.js:16990)
    at updateHostComponent (react-dom.development.js:17632)
    at beginWork (react-dom.development.js:19080)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)

3. The above error occurred in the <div> component:

at div
at App

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.

4. Uncaught Error: Element 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, or you might have mixed up default and named imports.

Check the render method of `App`.
    at createFiberFromTypeAndProps (react-dom.development.js:25058)
    at createFiberFromElement (react-dom.development.js:25086)
    at createChild (react-dom.development.js:13446)
    at reconcileChildrenArray (react-dom.development.js:13719)
    at reconcileChildFibers (react-dom.development.js:14125)
    at reconcileChildren (react-dom.development.js:16990)
    at updateHostComponent (react-dom.development.js:17632)
    at beginWork (react-dom.development.js:19080)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)

应用程序.js

import Display from './Comp/Display';
import Imex from './ImportExport/Import';

function App() {
  return (
    <div>
      <Display />
      <Imex />
    </div>
  );
}

export default App;

Display有一些数据在导入Imex之前显示在页面上。

导入.js

import a from "./Export";
const Imex = console.log(a); 
export default Imex;

导出.js

const a = "Name";
export default a;

在控制台中打印“名称”的 import.js 语句正在运行,但屏幕没有显示任何内容。

错误很简单,您没有从Import.jsExport.js导出反应组件。

但是,这会起作用:

应用程序.js

import Display from './Comp/Display';
import imex from './ImportExport/Import';

function App() {
  return (
    <div>
      <Display />
      {imex}
    </div>
  );
}

export default App;

导入.js

import a from "./Export";
const imex = a + " is john doe"; 
export default imex;

导出.js

const a = "Name";
export default a;

如果您在反应组件 (JSX) 中编写 javascript,则应在两个花括号之间编写 js。 而且它应该是一个表达式而不是一个语句

您正在尝试渲染一个组件Imex但它没有返回JSX 您可以尝试使用JSX代码吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM