0

I have my components folder with a component named Header. the Main JS file and Styles file. For some reason I am getting "import error: 'Header' is not exported from 'components/Header'"

Header.js:

const Header = () => {
  return (
    <div>
      <h2>The Header</h2>
    </div>
  );
};

export default Header;

Components/Header/index.js:

export * from "./Header";

App.js:

import { Header } from "components/Header";

function App() {
  return (
    <div className="App">
      <Header />
    </div>
  );
}

export default App;

Why is this not working?

2 Answers 2

5

It's because you do a default-export (export default Header) so you'll have to import it this way:

import Header from "components/Header"

If you want to use a named import (import { Header } from "components/Heaader";) you'll have to leave the "deafault" away from your export.

This StackOverflow post explains the difference well: Why and when to use default export over named exports in es6 Modules?

Sign up to request clarification or add additional context in comments.

2 Comments

Now i get this: 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.
Doing this: export { default } from "./Header"; works.
0

I just did copied the path by right clicking the file and selecting copy the path and did

import Header from "/Users/********/Downloads/keeper-app-part-1-starting/src/components/Header.jsx";

and it worked

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.