I'm currently switching my web app to react. The old one is located here.
What I'm trying to do is: when an user enter a player's username into the text field and submit, the app would redirect to the corresponding route (/:username), and the text field is cleared.
In the react version, this is what I'm doing currently: https://github.com/AVAVT/g0tstats-react/blob/master/src/components/SideBar/SearchBox.js
submit(event){
...
this.setState({
redirect : true
});
...
}
And
render(){
...
{
this.state.redirect && (
<Redirect to={`/${this.state.username}`} push />
)
}
}
Which kinda work. But there are 2 things I don't like about it:
- I'm rendering an element in order to redirect. It feels stupid and roundabout. It stinks of potential bug in the future.
- I'm stuck with the text field not cleared. Because I if I set state.username to null the
<Redirect />component will not redirect correctly. In fact I don't have any precise control over when the redirection occur (unless I do it in another roundabout way).
I have searched for alternative, but couldn't find one. withRouter doesn't work because <SearchBox /> is not a <Route /> and doesn't receive the history props.
So how can I say "redirect me to that place NOW" in react-router v4?
<SearchBox />? Presumably if its not the one getting the props since its not directly being routed to, then its parent which is being routed to should. - Chaim Friedmanthis.context.transitionTo(...)is how you do programmatic redirect if I'm reading what your question correctly. - 1252748<Route>s- AVAVTthis.contextincluded. - AVAVT