React-router: "exact" for "Redirect from"

Created on 24 Mar 2017  路  8Comments  路  Source: ReactTraining/react-router

Currently, it seems like the only way to redirect from the root to a particular sub-page is:

<Route exact path="/" render={() => <Redirect to="/dashboard" />} />

It would be a handy shortcut to be able to write (within a <Switch>, of course):

<Redirect from="/" exact to="/dashboard" />

It's possible this already works, but isn't documented.

Most helpful comment

Would you accept a docs PR to note that exact works in a Redirect nested in a Switch? The comment you link mentions it's unsupported that _any_ component works, but since Redirect is officially supported it make might sense to document that option. Just let me know!

All 8 comments

Would you accept a docs PR to note that exact works in a Redirect nested in a Switch? The comment you link mentions it's unsupported that _any_ component works, but since Redirect is officially supported it make might sense to document that option. Just let me know!

It could possibly be part of the <Switch children> docs, but it shouldn't be included in the <Redirect> docs.

Ah, I was going to finally do this, but you beat me to it! https://github.com/ReactTraining/react-router/pull/4849

馃帀 Thank you!

It could possibly be part of the <Switch children> docs, but it shouldn't be included in the <Redirect> docs.

Any particular reason for that? I only found this issue after googling some related terms, as I assumed that exact _wasn't_ supported after reading the docs for <Redirect />. Seems to me like that's the most obvious place to look?

@jmar777 I don't even really like having <Redirect from> in the Redirect.md documentation because it leads to people trying to do <Redirect from='/here' to='/there' /> outside of a <Switch>. I know that the docs say you shouldn't do that, but it still happens. If you come across rendering a <Redirect> inside of a <Switch> through the Switch.md documentation, then it is explicitly stated which props you can use:

When you include a <Redirect> in a <Switch>, it can use any of the <Route>'s location matching props: path, exact, and strict. from is just an alias for the path prop.

Maybe the Redirect.md docs could be rewritten to make it hard to miss seeing that the props only work in a <Switch>.

from: string

This prop is only valid when rendering a <Redirect> as a direct child of a <Switch>. See <Switch children> for more details.
A pathname to redirect from.

exact: boolean

This prop is only valid when rendering a <Redirect> as a direct child of a <Switch>. See <Switch children> for more details.
...

If you would like, you can open up a PR that makes these changes. If other collaborators are okay with it, I won't object.

@pshrmn

Maybe the Redirect.md docs could be rewritten to make it hard to miss seeing that the props only work in a <Switch>.

I like this approach. Inasmuch as from and exact are properties on the <Redirect /> component, that was where I thought to look, and I didn't even consider browsing the <Switch /> docs when I didn't see them there. I might be a bad example, but I'd wager I'm not alone there.

I'll look into submitting a PR later today. Thanks!

The "to" and "from" seems like a neat approach that would allow an easier oversight.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davetgreen picture davetgreen  路  3Comments

jzimmek picture jzimmek  路  3Comments

misterwilliam picture misterwilliam  路  3Comments

tomatau picture tomatau  路  3Comments

nicolashery picture nicolashery  路  3Comments