-
Notifications
You must be signed in to change notification settings - Fork 63
I can't seem to redirect using rr4 #11
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
Comments
Hi @nfatah , I'm not sure who's video that is but isn't mine so i'm not sure what they did. However, I can let you know how I handle redirects with React Router V4. Note, this was used in an early release of their V4 so it could be broken, but I doubt it. RR doesn't have a I accessed the function with context and exposed it globally like this: window._tt = this.context.router.transitionTo; However, there is a catch. This typically is accessed on the root of the page so if you put it on the 'home' page and the user never visits there, then it won't be available globally. To solve this I made a wrapper component called // expose a transition function so that actions can easily call it without having to use context
const ExposeRouter = React.createClass({
getInitialState: () => ({
isExposed: false,
}),
contextTypes: {
router: React.PropTypes.object.isRequired
},
componentDidMount() {
window._tt = this.context.router.transitionTo;
// defer rendering UI until context is set to prevent race conditions
setTimeout(() => this.setState({isExposed: true}), 0);
},
render() {
return this.state.isExposed ? this.props.children : null;
}
});
const MainLayout = React.createClass({
render() {
return (
<BrowserRouter>
<ExposeRouter>
<div>
<Header />
<div role="main">
<Match exactly pattern="/" component={Home} />
<Match exactly pattern="/login" component={Login} />
<Miss component={NotFound} />
</div>
</div>
</ExposeRouter>
</BrowserRouter>
);
},
});
export default MainLayout; and then in another folder I made a module like this to call the global function. This isn't necessary but it encapsulates the functionality I want in case I want to change routers in the future (not uncommon in React land lol): export const transitionToRoute = (url) => window._tt(url); |
I posted this question on SO after watching your VIDEOS series in which you were using
FlowRouter
withreact router
. However, this repo migrated to rr4 and I am trying to get a redirect onSubmit but it doesnt seem to work.Route redirects are working fine, as in user cannot access register/login when already loggedin, but when on the login page, user is not redirected.
There isnt a
FlowRouter.go()
equivalent in rr4 and I am forced to put the login check logic within the component and force rerender with react'sthis.forceUpdate()
- which doesn work for mehere is the SO question
The text was updated successfully, but these errors were encountered: