A
A
Alexey Chernyshov2019-01-11 15:57:10
React
Alexey Chernyshov, 2019-01-11 15:57:10

How to set up a dynamic route in react?

Hello, I ran into such a problem:
There are static uri and there are also dynamic uri.
It is necessary to implement the transition to dynamic uri, but at the same time check the existence of uri data, since they are taken from the server and can change. They also have nesting and start with dynamics. The city prefix will be added in the future.
Dynamic URLs are stored in the Redux store.
I tried to add: <Route path="/:category" component={Category} />but in this case, the transition to NotFound will not happen.
How can I implement this approach, thanks in advance

<Switch>
     <Route path="/" component={Home} />
     <Route path="/contact" component={Contact} />
     < ???? Category ???? />
     <Route component={NotFound} />
</Switch>

The example specified in the documentation did not fit or I did not figure it out to the end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Kirenkov, 2019-01-11
@WebDiamis

When you write `:category` both `/` and `/contact` fall under this route and you just don't get there. Try to put it in first place (but this is not accurate). I would recommend nesting dynamic parameters. Regarding dynamic loading, there is a good package - `react-loadable`, you may need it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question