Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Nested Routes in react-router-dom v4

I need multiple nested routes in react-router-dom

I am using v4 of react-router-dom

I've got my

import { BrowserRouter as Router, Route } from 'react-router-dom'; 

and I need the components to render like so

--- Login --- Home     --- Page 1     --- Page 2     --- Page 3 --- About --- etc 

The Home component contains a Header component that is common to Page1, Page2, and, Page3 components, but is not present in Login and About.

My js code reads like so

<Router>     <div>         <Route path='/login' component={Login} />         <Home>             <Route path='/page1' component={Page1} />             <Route path='/page2' component={Page2} />             <Route path='/page3' component={Page3} />         </Home>         <Route path='/about' component={About} />     </div> </Router> 

I expect the Login component to show only on /login When I request for /page1, /page2, /page3, they should contain the Home component and that page's content respectively.

What I get instead is the Login component rendered and below that Page1's component is rendered.

I'm pretty sure that I'm missing something very trivial or making a really silly mistake somewhere, and would appreciate all the help I could get. I've been stuck with this for the last two days.

like image 856
Aditya Talpade Avatar asked Mar 23 '17 18:03

Aditya Talpade


People also ask

Does react router allows for nested routes?

Nested Routes are a powerful feature. While most people think React Router only routes a user from page to page, it also allows one to exchange specific fragments of the view based on the current route.

How do I add nested routes to react?

Now, the last thing you need to do is tell React Router where in the parent Route ( Messages ) should it render the child Route ( Chats ). To do this, you use React Router's Outlet component. If the app's location matches the nested Route 's path , this Outlet component will render the Route 's element .

What are the components of react router v4?

The 'react-router' library is now split into three separate packages. react-router-dom: Designed for web applications. react-router-native: Designed for mobile applications. react-router-core: Can be used anywhere for core applications.


2 Answers

Use the url/path match obtained from props this.props.match.path to get the path that is set to a component.

Define your main routes as below

<Router>   <div>     <Route exact path="/" component={DummyIndex} /> {/* Note-1 */}     <Route path="/login" component={Login} />     <Route path="/home" component={Home} />     <Route path="/about" component={About} />     <Route path="/etc" component={Etc} />   </div> </Router> 

Then in Home Component, define your routes

class Home extends Component {   render() {     return <div>       <Route exact path={this.props.match.path} component={HomeDefault} />       <Route path={`${this.props.match.path}/one`} component={HomePageOne} />       <Route path={`${this.props.match.path}/two`} component={HomePageTwo} />     </div>   } } 

The defined routes are as below

  • /login
  • /home
  • /home/one
  • /home/two
  • /about
  • /etc

If you want to nest routes further in HomePageOne like /home/one/a and /home/one/b, you can proceed the same approach.

Note-1: If you don't want further nesting, just set the route with prop exact.

EDIT (May 15, 2017)

Initially, I've used props.match.url and now I changed it to props.match.path.

We can use props.match.path instead of props.match.url in Route's path so that if you use path params in top level routes, you can get it in inner (nested) routes via props.match.params.

If you don't you any path params, props.match.url is enough

like image 191
Ram Babu Avatar answered Sep 29 '22 08:09

Ram Babu


Use Switch component in router v4

<Router> <Switch>   <Route path='/login' component={Login} />   <Route path='/about' component={About} />   <Home>     <Route component={({ match }) =>       <div>         <Route path='/page1' component={Page1} />         <Route path='/page2' component={Page2} />         <Route path='/page3' component={Page3} />       </div>     }/>   </Home> </Switch> 

export default class Home extends Component { render() {     return (       <div className="Home">           { this.props.children }       </div>     )   } } 

I think this code shows the basic idea of using component.

like image 36
Igor Stetsiura Avatar answered Sep 29 '22 07:09

Igor Stetsiura