-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redirect. #92
Comments
We don't have a declarative way to do this currently, the manual way to do this would be to insert a fallback route in a hierarchy and fire a new route based on the current location. Do you mind if I ask what your use case is? |
Do you think Is there an architectural reason why it hasn't been implemented, or is it a matter of time and priorities? |
I have an application with several pages which are peers - i.e. I want the root route If we used |
I see. That makes sense to me. It is just a matter of time and priorities. I think it should be rather simple to create a component that reads the current URL on mount, reaches out to its parent router and reroutes. Would be very happy to accept a PR.
|
Just posting my interim solution, in case it helps anyone.
Note: This is only "mostly" functional. If for some reason if you request the redirected page directly (i.e. by typing in the URL), then it change the page location, but will display the blue link instead of showing the content of the new page. I have no idea why this is. |
@chrisdew Have you found a solution to this particular problem yet? I face the same problem, page location being correct, but showing the wrong component. |
@STRML I've started to work on this but I suspect I'm going about this in the wrong way. I've gotten the following scenario to work:
But when interpolation is involved, the redirect takes the literal path template (not interpolated):
The relevant code here: Thoughts? |
Not sure if I am abusing |
In my application this component handles redirects by just import React from 'react';
import Router from 'react-router-component';
import Loading from 'common/components/Loading';
export default React.createClass({
displayName: 'Redirect',
mixins: [Router.NavigatableMixin],
propTypes: {
force: React.PropTypes.bool,
location: React.PropTypes.string
},
performRedirect (props) {
let loc = props.location;
let location = global.location;
let currentFragment = location && location.hash;
if (props.force) {
console.debug('Forceful redirect to: %s', loc);
return location.replace(loc);
}
if (loc && loc.indexOf('#') === -1 && currentFragment) {
loc = loc +
(currentFragment.charAt(0) !== '#' ? '#' : '') +
currentFragment;
}
// let routes = this.context.router.props.children.map(x=>x.props.path || 'default');
// console.debug('Redirecting to %s, routes: %o', loc, routes);
console.debug('Redirecting to %s', loc);
this.navigate(loc, {replace: true});
},
startRedirect(p) {
clearTimeout(this.pendingRedirect);
this.pendingRedirect = setTimeout(()=> this.performRedirect(p), 1);
},
componentDidMount () {
this.startRedirect(this.props);
},
componentWillReceiveProps (props) {
this.startRedirect(props);
},
render () {
return (<Loading message="Redirecting..."/>);
}
}); |
Works for me; I ended up taking your code and turning it into a mixin that wraps |
For a related redirect issue #24 (comment), here's what I use that so far works even when directly typing the url: https://gist.github.com/saidimu/2758402f7916f4e35dee
@chrisdew @jbach I wonder if the issue you faced is because of not rendering |
@saidimu Redirects replace the current route... you should add |
@jsg2021 Where is the |
@saidimu It's not documented. Its internal implementation. You are calling parts of the router that were not meant to be exposed. (much like The only difference will be your history in the browser will not include the route the redirect was on... if The |
I'm looking for react-router-component's equivalent of https://github.com/rackt/react-router 's
<Redirect ...>
.If there isn't one, how do most users of react-router-component currently do their redirects?
The text was updated successfully, but these errors were encountered: