0

I am learning React.js. I need to know how can I display more than one component in App.js. I have 2 pages which are Home.js and About.js.

After run the code Just click on About us then you will get only About Page text. But I have About Team and About content too in the About.js file. That is not displaying. I import the

import { About, AboutTeam, AboutContent } from "./Pages/About";

but never use till now because I don't know where should I add AboutTeam, AboutContent. Please check my App.js file. I just need when the user clicks on About us then It will display all the components which I have in About.js.

I added example here https://codesandbox.io/s/happy-almeida-t6q7w?file=/src/App.js

I am getting

enter image description here

This is my expected output

enter image description here

One more doubt, I am using the below code so is this code is the correct way to use?

Would you help me out with this?

Home.js

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import './css/Style.css';

const Home=()=>{
  return(
    <div className="">
        <h2>Home page</h2>
    </div>
  );
}
export default Home;

About.js

    import React from 'react';
    import 'bootstrap/dist/css/bootstrap.min.css';
    import './css/Style.css';

    const About=()=>{
      return(
        <div className="">
            <h2>About page</h2>
        </div>
      );
    }
const AboutTeam = () => {
  return (
    <div className="">
      <h2>About Team dummy text</h2>
    </div>
  );
};

const AboutContent = () => {
  return (
    <div className="">
      <h2>About content dummy text</h2>
    </div>
  );
};

export { About, AboutTeam, AboutContent };

App.js

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import './css/Style.css';
import HeaderMenu from './components/Header';
import Home from './pages/Home';
import { About, AboutTeam, AboutContent } from "./Pages/About";
import Footer from './components/Footer';
import { BrowserRouter, Route, Switch } from 'react-router-dom';


const App=()=>{
  return(
    <BrowserRouter>
      <HeaderMenu />
    <div className="">
      <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/about"  component={About} />
        </Switch>
    </div>
    <Footer />
    </BrowserRouter>
  );
}
export default App;

Index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

import * as serviceWorker from './serviceWorker';

ReactDOM.render(<App />,document.getElementById('root'));

serviceWorker.unregister();
user9437856
  • 2,360
  • 2
  • 33
  • 92
  • 1
    Does this answer your question? [exporting multiple modules in react.js](https://stackoverflow.com/questions/46039976/exporting-multiple-modules-in-react-js) – emrhzc May 02 '20 at 11:33

5 Answers5

1

You can write in App.js

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import './css/Style.css';
import HeaderMenu from './components/Header';
import Home from './pages/Home';
import { About, AboutTeam, AboutContent } from "./Pages/About";
import Footer from './components/Footer';
import { BrowserRouter, Route, Switch } from 'react-router-dom';

const AboutPage = () => (
  <>
    <About />
    <AboutTeam />
    <AboutContent />
  </>
);

const App=()=>{
  return(
    <BrowserRouter>
      <HeaderMenu />
      <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/about" component={AboutPage} />
        </Switch>
      <Footer />
    </BrowserRouter>
  );
}
export default App;
Drylchik
  • 94
  • 4
  • So every time, if I create component then I have to declare like this import { About, AboutTeam, somemore1,somemore2 } from './About.js'? – user9437856 May 02 '20 at 11:42
  • I am really appreciate your support here. but I am not getting. Please check my App code in the question. – user9437856 May 02 '20 at 12:03
  • if you want to use AboutTeam Component you need to use it because in your Code AboutTeam Component is not used anywhere; you only imported it – Drylchik May 02 '20 at 12:10
  • Can you check my code codesandbox.io/s/happy-almeida-t6q7w?file=/src/Pages/About.js – user9437856 May 02 '20 at 15:34
  • your Footer.js file is empty – Drylchik May 02 '20 at 15:59
  • Yes, I know my footer is empty now I added some code, but that is not my issue, I update my full code in the above link. My issue is I am not able to use them more than one component of the About.js. Please check my About.js file I have 3 component but it is displaying only one. – user9437856 May 02 '20 at 17:17
  • Just click on About us then you will get only About Page. But I have About Team and About content too in the About.js file. IN the App.js file I import all 3 – user9437856 May 02 '20 at 17:33
  • Because you don't use About Team Component. You only imported it but you don't use it; I wrote it above – Drylchik May 02 '20 at 17:35
  • Yes, So where should I use that. That is my issue – user9437856 May 02 '20 at 17:36
  • It depends on where you want to use those compoents – Drylchik May 02 '20 at 17:37
  • I just need when user clicks on About us then all should be display. I am learning the React. Please guide me. – user9437856 May 02 '20 at 17:39
  • then you do not need to export AboutTeam Component const AboutTeam=()=>{ return(

    About Team

    ); } const About=()=>{ return( <>

    About page

    > ); }
    – Drylchik May 02 '20 at 17:41
  • But why? I have 3 section my about page each section I have to display. – user9437856 May 02 '20 at 17:44
  • I checked your code but that is not a good way to use it. I updated my code in the question. Please check it once. – user9437856 May 02 '20 at 19:04
  • Yes, This time it's working. I am really appreciate your answer and support. Thanks for the help. Upvote from my aide. – user9437856 May 02 '20 at 20:30
0

If you want to import {About, AboutTeam} from ..., then you need to export 2 variables:

export const About = ...
export const AboutTeam = ...

It's not advisable to have too many components in 1 file, but if you really want to import all, that is also possible:

import * as About from './About.js'

... <About.About /> ... <About.AboutTeam /> ...
Aprillion
  • 21,510
  • 5
  • 55
  • 89
  • So every time, if I create component then I have to declare like this import { About, AboutTeam, somemore1,somemore2 } from './About.js'? – user9437856 May 02 '20 at 11:37
  • you don't *have* to, you can use `import *` as per my updated answer... but the explicit imports are more popular. – Aprillion May 02 '20 at 11:46
  • " It's not advisable to have too many components in 1 file," Why I am asking this because I have 6-7 sections in my about page and each layout is different. – user9437856 May 02 '20 at 11:46
  • anything you don't like about `import {...1-7...} from './About'`? you can use newlines inside the statement to make it more readable... – Aprillion May 02 '20 at 11:49
  • I added my code in About.js and export like export {About, AboutTeam, AboutContent} then I added import {About, AboutTeam,AboutContent} from './pages/About'; Now I am still getting only About content. I am not getting the AboutTeam,AboutContent – user9437856 May 02 '20 at 11:59
  • if you have a different question from what you asked in your question, feel free to ask a new question. I won't be able to help you without a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Aprillion May 02 '20 at 12:05
  • Wait a min I am sharing my code link for better understant – user9437856 May 02 '20 at 12:15
  • I added my code here : https://codesandbox.io/s/happy-almeida-t6q7w?file=/src/Pages/About.js – user9437856 May 02 '20 at 12:32
  • you never use the imported `AboutTeam` and `AboutContent` - I have no idea what you expect to happen :( – Aprillion May 02 '20 at 12:38
  • I used in the App.js import { About, AboutTeam, AboutContent } from "./Pages/About"; – user9437856 May 02 '20 at 12:39
  • yes, you imported them. but what do you want to *do* with them? unused import is similar to unused variable, if you write `let x = 2` and then never *use* `x` anywhere else, it's equally useless as importing variables without actually using them - and equally mysterious to the readers of the code about your reasons *why* you wanted to import it – Aprillion May 02 '20 at 13:03
  • Yes, now I need to know where I have to use that.......Let me explain again. In the about page I have 3 component and all are exported. Then I import in the App.js Now I need to now how can I access that. Please check my App.js code – user9437856 May 02 '20 at 13:12
0
// About.js

const AboutTeam = () => {
  return (
    <div className="">
      <h2>About Team</h2>
    </div>
  );
};
const About = () => {
  return (
    <div className="">
      <h2>About page</h2>
    </div>
  );
};

export { About, AboutTeam };

Then import it as

import { About, AboutTeam } from './About.js'
Jagrati
  • 11,474
  • 9
  • 35
  • 56
  • So every time, if i create component then I have to declare in the import? – user9437856 May 02 '20 at 11:36
  • you will have to export it once like `export { About, AboutTeam }` and while importing any of `About` or `AboutTeam` , it can be imported as named import.. like: `import { About } from "./About.js` – Jagrati May 02 '20 at 11:37
  • So every time, if I create component then I have to declare like this import { About, AboutTeam, somemore1,somemore2 } from './About.js'? – user9437856 May 02 '20 at 11:44
  • yes, if you are using all the components ` About, AboutTeam, somemore1,somemore2` in the file you are importing into... then import eveything – Jagrati May 02 '20 at 11:48
  • I added my code here: Please check it https://codesandbox.io/s/happy-almeida-t6q7w?file=/src/Pages/About.js – user9437856 May 02 '20 at 12:34
  • can help me in this? Please – user9437856 May 02 '20 at 17:54
  • Just click on About us then you will get only About Page. But I have About Team and About content too in the About.js file. I haven't use About Team Component. I just import. – user9437856 May 02 '20 at 17:56
0

you can create more than one component in one file like this:

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import './css/Style.css';

export const AboutTeam =() => {
    return (
     <div>About Team Page </div>
    )
}

export const About =()=>{
  return(
    <div className="">
        <h2>About page</h2>
    </div>
  );
}

all things are looking fine, but you should not leave className empty and inside BrowserRouter there should be only one wrapper so you should wrap all elements inside a div.

Bhuwan Chandra
  • 294
  • 1
  • 9
0

use export instead of export default to export both the component from the same file (About.js)

//About.js

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import './css/Style.css';

const About=()=>{
  return(
    <div className="">
        <h2>About page</h2>
    </div>
  );
}

const AboutTeam=()=>{
  return(
    <div className="">
      <h2>About Team</h2>
    </div>
  );
}

export {About, AboutTeam};

and then import it in the file you need,

import {About, AboutTeam} from "./About.js";

Apart from the solution, one more thing to keep in mind is

  • component exported using export default, is imported like

    import About from "./path of file";

  • component exported using export, is/are imported like

    import {About, AboutTeam} from "./path of file";