Home > database >  Errors while importing module in React
Errors while importing module in React

Time:12-28

The app was running good without any errors until I imported Imex, it showed 4 errors as follows

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)

App.js

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

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

export default App;

The Display has some data which was displayed on the page before importing Imex.

Import.js

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

Export.js

const a = "Name";
export default a;

The import.js statement to print "Name" in the console is working but the screen is not showing anything.

CodePudding user response:

The error is simple you are not exporting a react component from both Import.js and Export.js.

But, this would work:

App.js

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

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

export default App;

Import.js

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

Export.js

const a = "Name";
export default a;

If you are writing javascript inside a react component (JSX), you should write js inbetween two curly braces. And also it should be an expression not a statement

CodePudding user response:

You're trying to render a component Imex but it has returned no JSX. Can you try with the JSX code?

  • Related