Home > OS >  passing regex to path prop of Route works with warning
passing regex to path prop of Route works with warning

Time:11-14

              <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?

CodePudding user response:

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

  • Related