1

Nested routing is not working. 'Landing' And 'home' components showing properly but nested components are not rendering.

The layout component is directly imported in app.js there is no error in the console but nested components such as 'Feed' and 'Myprofile' are not rendering.

see the code

import React from 'react';
import { Route, Switch, BrowserRouter as Router} from 'react-router-dom';
import Sidebar from '../componants/Sidebar'
import Navbar from '../componants/Navbar';
import PathRouter from '../Routers/PathRouters'
import Landing from '../pages/Landing';

const Home=()=>{
    
    return(
        <>
            <div className="container-fluid">
                <Navbar />

                <div className="rows row">
                    <div className='col-3'>
                        <Sidebar />
                    </div>
                    <div className="col-9 ">
                        <div className="content">                            
                            <Switch>
                                {PathRouter.map((prop, key) => {
                                    return (
                                        <Route
                                           exact path={prop.path}
                                            component={prop.component}
                                            key={key}
                                        />
                                    );
                                }
                                )}
                            </Switch>
                        </div>
                    </div>                
                </div>
            </div>
        </>
    )
}
const Layout = () => {      
    return (
        <>
            <Router>            
                <Switch>
                    <Route exact path='/' component={Landing} />
                    <Route exact path='/home' component={Home} />
                </Switch>            
            </Router>               
        </>
    )

}
export default Layout;

This is Pathrouter.js

import Feed from '../pages/Feed';
import Answer from '../pages/Answer';
import Addquestion from '../componants/Addquestion';
import Myprofile from '../componants/Myprofile';

var PathRouter = [
    {
      path: '/home/feed',
      name: 'Feed',
      component: Feed
    },
    {
      path: '/home/answer/:q_id',
      name: 'answer',
      component: Answer
    },
    {
      path: '/home/addquestion',
      name: 'addquestion',
      component: Addquestion
    },
    {
      path: '/home/myprofile',
      name: 'myprofile',
      component: Myprofile
    }
]
export default PathRouter;
2
  • 1
    In the Layout component the line <Route exact path='/home' component={Home} /> is breaking your nested routes. For the nested route to render the parent route must also render, since '/home/feed' does not exactly match '/home' the parent route will not be rendered. Remove exact from the '/home' route. Commented Mar 3, 2021 at 5:01
  • Thank you so much sir Problem is solved. @JacobSmit Commented Mar 3, 2021 at 5:06

1 Answer 1

1

In the Layout component the line <Route exact path='/home' component={Home} /> is breaking your nested routes. For the nested route to render the parent route must also render, since '/home/feed' does not exactly match '/home' the parent route will not be rendered. Remove exact from the '/home' route. – Jacob Smit

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

Comments

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.