简体   繁体   中英

Can't render only one Route in react-router-dom

I made a router, but upon request /user/add, both components are rendered. I need to only display the UserAdd component.

<Switch>
  <Route exact path="users/add" component={UserAdd} />
  <Route exact path="users/:id" component={UserPreview} />
</Switch>

add exact to route,

<Route exact path="/users/add" component={UserAdd} />

https://reacttraining.com/react-router/web/api/Route/exact-bool

exact keyword is used only when you want to render a component when there is an exact match of route.

When you have multiple routes then use exact to visit specific page.

<Route exact path="/users/add" component={UserAdd} />

For better understanding between <Route exact path="/" /> and <Route path="/" />

check difference

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