B
B
bdFregat2019-09-10 11:37:23
React
bdFregat, 2019-09-10 11:37:23

Is it possible to use regex variables in react-router's path?

Actually there is a working code:

<Route path='/section/:id(\d+)' component={EditSection} />

And its non-working counterparts:
<Route path={'/section/:id(\d+)'} component={EditSection} />

const pathVariable = '/section/:id(\d+)';
<Route path={pathVariable } component={EditSection} />

The path without a regular expression (\d+) works in both cases, with a regular expression it does not work with curly braces
. What is the fundamental difference between passing a parameter through a string and through curly braces? Why does the regex work in one case and not in the other?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bdFregat, 2019-09-13
@bdFregat

As a result, the problem turned out to be that curly braces are escaped, as opposed to writing through quotes. The solution turned out to be as simple as possible, write \\d instead of \d.

P
Pavel Didenko, 2019-09-10
@Dasslier

Because the regular expression needs to be escaped, you are now passing it as a simple string. Try to navigate by id with regular expression characters, you should have an EditSection component open.
Take a look here Regular expressions several chapters - and you will figure out your problem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question