Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue with how to render with react router

I have this specific case where if the user routes to say topics/rendering, I need to show him a seperate Component and when user routes to topics/10, I need to show him another Component. The problem I am facing right now is that even when I do topics/rendering, I see both of the components being rendered on screen. Also when user routes to topics/math/10, I need to route him to a different page.

Here is the routing part in my App.js (default App.js)

<div className="App">
    <Router>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route exact path="/topics" component={Topics} />
      <Route exact path ="/topics/rendering" component={Description}/>
      <Route  exact path="/topics/:id" component={Topic} />
      <Route path="/topics/:description/:id" component={TopicExtended}/>
    </Router>
  </div>
like image 959
dfsdigging Avatar asked Oct 17 '22 05:10

dfsdigging


1 Answers

You want to wrap the Route components in a Switch component so that only one of them will be rendered at a time.

<div className="App">
  <Router>
    <Switch>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/topics" component={Topics} />
      <Route path="/topics/rendering" component={Description} />
      <Route path="/topics/:id" component={Topic} />
      <Route path="/topics/:description/:id" component={TopicExtended} />
    </Switch>
  </Router>
</div>
like image 147
Tholle Avatar answered Oct 21 '22 02:10

Tholle