最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论