1

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>
  • 1
    If you have rendered the routes in a `Switch` as in the question, `exact` is not required, can you recheck if this indeed the configuration being used? – Agney Jan 31 '20 at 05:26

2 Answers2

3

add exact to route,

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

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

Alex
  • 3,485
  • 1
  • 11
  • 18
  • I added `exact` to both Route. But when we turn to /users/add, both UserAdd and UserPreview are rendered. – Maks Endless Jan 31 '20 at 05:23
  • @Maks Endless , could you please share what was the problem? I think it was missing `/`, isn't it? – Alex Jan 31 '20 at 05:57
0

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

akhtarvahid
  • 6,733
  • 2
  • 14
  • 21