Override dynamic route if exact match is found

Viewed 1169

I'm pretty new to using v4 of React Router, and am currently struggling with the following scenario

<Route exact path="/campaign/new" component={CampaignEditor}/>
<Route exact path="/campaign/:id" component={Campaign}/>

I want the exact match /campaign/new to go render CampaignEditor. In all other cases, I want to render Campaign with the dynamic id as a parameter.

I can of course render the editor in the campaign component if the parameter equals "new", but is there a way to do it with the router?

1 Answers

Make use of Switch and you can render the routes like

<Router>
     <Switch>
          <Route exact path="/campaign/new" component={CampaignEditor}/>
          <Route path="/campaign/:id" component={Campaign}/>
     </Switch>
</Router>
Related