If you have a <Redirect>
inside a <Route>
, you'll get a location
and can do: <Redirect search={props.location.search} hash={props.location.hash} ...
.
However, when directly inside a top level <Switch>
, the <Redirect>
doesn't have any props.location
to access search
and hash
on.
Is there no way to preserve the query string and hash fragment, in a <Redirect>
directly after a top level <Switch>
(no <Route>
higher up in the tree)?
Example: (Typescript)
Router({},
Switch({},
Redirect({ path: '/', to: '/latest', exact: true }))))
Changing to: '/latest'
to to: { pathname:... search:.. hash:.. }
doesn't work because there's no props.location
available to access the original .search
and .hash
on.
Here (https://github.com/ReactTraining/react-router/issues/5818#issuecomment-384934176 ) the devs says the preserve-query-and-hash problem has been solved but I cannot find anything that works in the above case:
> any option the save query string on redirect?
> it will be implemented in 4.3.0. See release notes here: https://github.com/ReactTraining/react-router/releases/tag/v4.3.0-rc.1
Those release notes links to: https://github.com/ReactTraining/react-router/pull/5209 which doesn't mention anything that seems to work.
Maybe they meant only for <Redirect>
inside a <Route>
already? Then, one can do something like:
Route({ path: ..., render: (props) => function() {
Redirect({ to: { pathname:... search: props.location.search, ... } ...}));