C
C
c00ldev2020-01-09 12:09:24
React
c00ldev, 2020-01-09 12:09:24

How to properly organize routing in an application?

Routing is organized through react-router-dom. The structure is the following:

<Route exact path="/goods" component={ Goods } />
<Route path="/goods/:id" component={ Details } />
<Route exact path="/new" component={ NewGood } />

I want to ensure that the NewGood component is available on the /goods/new route, i.e.:
<Route exact path="/goods/new" component={ NewGood } />

The problem is that with this route, /new becomes /:id. How to solve the problem?
Reproduced the issue in the sandbox:
https://codesandbox.io/s/still-tdd-dcsir

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolay Matyushkin, 2020-01-09
@c00ldev

Your answer to the question in the documentation
I'll explain a little.
1. Create a route at the "/goods" url, where the Goods component will be rendered (you have already created it)
2. In the Goods component, organize a new routing.

A
Alexey Ukolov, 2020-01-09
@alexey-m-ukolov

The order is important, emnip:

<Route exact path="/goods" component={ Goods } />
<Route exact path="/goods/new" component={ NewGood } />
<Route path="/goods/:id" component={ Details } />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question