0

I have made 3 components

1)Navbar

2)Content

3)Pagination

On home page I want to display all 3 components. I have made another component Matchinfo which should get displayed when we click on view stats button (see screenshot for more clarification) .

In app.js how should I make use of Routes so that 3 components will get display on home page i.e localhost:3000/ and when I click on view stats button it should render component Matchinfo.

In app.js :

import React, { Component } from 'react';
import { Route, NavLink, HashRouter } from "react-router-dom";
import './App.css';
import Navbar from './components/navbar';
import Content from './components/content';
import Pagination from './components/pagination';
import Matchinfo from './components/matchinfo';

class App extends Component {
  render() {
    return (
        <div className="App">

          <div className="content">
            <Route path="/" component={Navbar, Content, Pagination}/>
            <Route path="/match" component={Matchinfo}/>
          </div>
        </div>
    );
  }
}

export default App;

enter image description here

2 Answers 2

1

You no need to call these components in app.js using routes. I would request you to create sepearte component like Home(see example below home.js).

Then, In app.js call Home component

import Home from './components/home'; 

<Route path="/" component={Home}/>

create home.js under components

Call Navbar, Content annd Pagination components in Home component

import React, {Component} from "react";
import Navbar from './components/navbar';
import Content from './components/content';
import Pagination from './components/pagination';

class Home extends Component {
  constructor(props) {
    super(props);        
    this.state = {

    }
  }

  componentWillMount() {

  }

  render() {
    return (
      <div>  
        <Navbar/>
        <Content />
        <Pagination/>
      </div>
    )
  }
}

export default Home;

Since you want to display Navbar, Content annd Pagination components in home page so do it like above way. Here Home is your parent component and Navbar, Content annd Pagination are child components to Home.

One route is enough mostly for one web page and in React most of times you will play with child components. You no need to configure all the components with routes.

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

4 Comments

So then I should inject Home component for path= "/" but now when I click on button view stats it should display Navbar and matchinfo component how can I add both as 2nd route.
Yes you have to inject Home for path='/'. You want to display Matchinfo when you click on view stats isn't it?
Yes onclick I want to display matchinfo and navbar component.
Navbar will be fixed. I mean Navbar will be displayed. I want to see your Content component code.
1

There are several ways achieving the result. The first one is using render method for home Route. Also, use exact attribute of Route to ensure the location is matched exactly.

import React, { Component } from 'react';
import { Route, NavLink, HashRouter } from "react-router-dom";
import './App.css';
import Navbar from './components/navbar';
import Content from './components/content';
import Pagination from './components/pagination';
import Matchinfo from './components/matchinfo';

class App extends Component {
  render() {
    return (
        <div className="App">
          <div className="content">
            <Route path="/" render={(props) => (
                <React.Fragment>
                      <Navbar/>
                      <Content />
                      <Pagination/>
                <React.Fragment/>
            )} exact/> 
            <Route path="/match" component={Matchinfo} exact/>
          </div>
        </div>
    );
  }
}

export default App;

The second one, create auxiliary component Home, for example, and include it in your route, like this:

<Route path="/" component={Home} exact/>

5 Comments

I could not get The second one, create auxiliary component Home is it needed to create HOME component
@Ibhor I am new to REACTJS can you tell me some easy way to do it as I am not aware of React.Fragment
sure, it's simple, just replace React.Fragment with <div> tag
@Ibhor can I do like this <Route path="/" component={Navbar , Pagination, Content}/>
Why ? Will this work <Route exact path="/" component={Navbar , Pagination, Content}/> If possible can you edit your answer in some easy format I am not able to understand it.

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.