简体   繁体   中英

React-router-dom, route component not render

i want to create a tab using react-router-dom . so in my parent route, i set my route to the profile page.

<Router history={history}>
     <div>
       <Route path="/profile" exact component={ProfilePageComponent}/>
    </div>
</Router>

then, in my ProfilePageComponent , i set the child route

<div className="profile-page-container">
   <div className="height-controler">
      <SidebarProfileComponent/>
      <Switch>
          <Route path={`/profile/change-password`} component={ChangePasswordComponent}/>
      </Switch>
   </div>        
</div>

when i go to the /profile path, the SidebarProfileComponent is showing but the ChangePasswordComponent isnt showing.

Since you have an exact attribute defined on the parent route, when you visit /profile/change-password , the parent Route itself doesn't match and hence there is no question of the child route matching.

If you remove the exact prop from the parent route, /profile matches /profile/change-password and hence ProfileComponent gets rendered and then the child Route is matched

Remove the exact prop from parent

<Router history={history}>
     <div>
       <Route path="/profile" component={ProfilePageComponent}/>
    </div>
</Router>

PS Always remember if you have nested Routes, you must not use exact attribute, instead use Switch component for multiple Routes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM