javascript - How to export multiple components from an index file in React? - Stack Overflow

Here is my index.jsx file:import Navbar from '.NavbarNavbar';import Greeting from '.

Here is my index.jsx file:

import Navbar from './Navbar/Navbar';
import Greeting from './Greeting/Greeting'
import Project from './Project/Project';
import About from './About/About';
import Contact from './Contact/Contact';
import Footer from './Footer/Footer';

export default {
  Navbar,
  Greeting,
  Project,
  About,
  Contact,
  Footer,
};

And my App.jsx file:

import React from 'react';
import {Navbar} from './ponents';

// import Navbar from './ponents/Navbar/Navbar';

const App = () => {
  return (
    <div>
      <Navbar />
    </div>
  )
}

export default App;

When I call to import Navbar through the index.jsx file, I got this error:

Uncaught SyntaxError: The requested module */sre/ponents/index. isx? App.isx:4 t=1672200538218' does not provide an export named "Navbar'

What wrong I got if I call Navbar through index file? Thank you so much!

How can I call to import many ponents througnt index file.

Here is my index.jsx file:

import Navbar from './Navbar/Navbar';
import Greeting from './Greeting/Greeting'
import Project from './Project/Project';
import About from './About/About';
import Contact from './Contact/Contact';
import Footer from './Footer/Footer';

export default {
  Navbar,
  Greeting,
  Project,
  About,
  Contact,
  Footer,
};

And my App.jsx file:

import React from 'react';
import {Navbar} from './ponents';

// import Navbar from './ponents/Navbar/Navbar';

const App = () => {
  return (
    <div>
      <Navbar />
    </div>
  )
}

export default App;

When I call to import Navbar through the index.jsx file, I got this error:

Uncaught SyntaxError: The requested module */sre/ponents/index. isx? App.isx:4 t=1672200538218' does not provide an export named "Navbar'

What wrong I got if I call Navbar through index file? Thank you so much!

How can I call to import many ponents througnt index file.

Share Improve this question edited Dec 28, 2022 at 22:00 Youssouf Oumar 46.6k16 gold badges103 silver badges105 bronze badges asked Dec 28, 2022 at 4:27 nhontnhont 431 silver badge4 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

Instead of importing them and then exporting them, you could directly export them as below. But for this to work you would want to use named exports, like export function Navbar() instead of export default function Navbar().

export { Navbar } from "./Navbar/Navbar";
export { Greeting } from "./Greeting/Greeting";
export { Project } from "./Project/Project";
export { About } from "./About/About";
export { Contact } from "./Contact/Contact";
export { Footer } from "./Footer/Footer";

But if you would rather keep your default export, you should be doing as below instead of what you have:

export { default as Navbar } from "./Navbar/Navbar";
export { default as Greeting } from "./Greeting/Greeting";
export { default as Project } from "./Project/Project";
export { default as About } from "./About/About";
export { default as Contact } from "./Contact/Contact";
export { default as Footer } from "./Footer/Footer";

You can learn more about export on mdn.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745285180a4620517.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信