Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

passing regex to path prop of Route works with warning

              <Route exact path={/^\/+$/g}>
                <Redirect to="/home" />
              </Route>

does work but:

Warning: Failed prop type: Invalid prop `path` supplied to `Route`.
Route@http://localhost:3000/static/js/vendors~main.chunk.js:789274:29
App@http://localhost:3000/static/js/main.chunk.js:666:79
Provider@http://localhost:3000/static/js/vendors~main.chunk.js:785880:15

Are we not supposed to pass a regex to path?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

React-router-dom path props use path-to-rexexp. The path prop can be either a string or an array of strings.

If I’m not mistaken you are wanting to match and redirect from any path with one or more "/" repeated characters ("/", "//", "///", etc…). For this the path would be "/+". You can also specify the from prop on the Redirect component so there’s no need to be matched in a Route first if used inside a Switch.

path-to-regexp one-or-more

<Redirect from="/+" to="/home" />

Note the Redirect to prop takes only a string.

Redirect

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading