admin管理员组文章数量:1290059
Trying to use browserHistory.push
method to change my route programmatically.
It will change the route (per browser address bar) but doesn't update the view.
Here's my code App.jsx
const AppStart = React.createClass({
render: function() {
return (
<MuiThemeProvider>
<Router history={hashHistory}>
<Route path="/" ponent={Main}>
<Route path="experiences" ponent={Experiences} />
<Route path="people" ponent={Profiles} />
<Route path="login" ponent={Login} />
<IndexRoute ponent={Home}/>
</Route>
</Router>
</MuiThemeProvider>
);
}
});
ReactDOM.render(
<AppStart />,
document.getElementById('app')
);
Component:
handleLoginRedirect(e) {
browserHistory.push('/experiences');
},
render() {
return (
<div className='row'>
<div className='col-sm-10 col-sm-offset-1'>
<form role='form'>
<RaisedButton label="Redirect" onClick={this.handleLoginRedirect} />
</form>
</div>
</div>
);
Trying to use browserHistory.push
method to change my route programmatically.
It will change the route (per browser address bar) but doesn't update the view.
Here's my code App.jsx
const AppStart = React.createClass({
render: function() {
return (
<MuiThemeProvider>
<Router history={hashHistory}>
<Route path="/" ponent={Main}>
<Route path="experiences" ponent={Experiences} />
<Route path="people" ponent={Profiles} />
<Route path="login" ponent={Login} />
<IndexRoute ponent={Home}/>
</Route>
</Router>
</MuiThemeProvider>
);
}
});
ReactDOM.render(
<AppStart />,
document.getElementById('app')
);
Component:
handleLoginRedirect(e) {
browserHistory.push('/experiences');
},
render() {
return (
<div className='row'>
<div className='col-sm-10 col-sm-offset-1'>
<form role='form'>
<RaisedButton label="Redirect" onClick={this.handleLoginRedirect} />
</form>
</div>
</div>
);
Share
Improve this question
edited Nov 18, 2016 at 15:40
DeBraid
9,4415 gold badges33 silver badges43 bronze badges
asked Aug 12, 2016 at 1:32
zsaynzsayn
2171 gold badge3 silver badges9 bronze badges
1
- 1 Actually just figured it out, can't believe it. I didn't see that I used hashHistory instead of browserHistory inside <Router>, changing to browserHistory fixed everything – zsayn Commented Aug 12, 2016 at 1:54
2 Answers
Reset to default 4Your router configuration uses hashHistory
while you're pushing onto browserHistory
.
It's easy to miss something like this and it's understandable.
- Replace
hashHistory
withbrowserHistory
in yourRouter
like so:
<Router history={browserHistory}>
Full Snippet:
const AppStart = React.createClass({
render: function() {
return (
<MuiThemeProvider>
<Router history={browserHistory}>
<Route path="/" ponent={Main}>
<Route path="experiences" ponent={Experiences} />
<Route path="people" ponent={Profiles} />
<Route path="login" ponent={Login} />
<IndexRoute ponent={Home}/>
</Route>
</Router>
</MuiThemeProvider>
);
}
});
If you're using the newest react-router api you'll need to use:
this.props.history.push('/path/goes/here');
You may need to bind this to your function when accessing this.props (note: may):
onClick={this.handleLoginRedirect.bind(this)}
Please refer to the following question for all the information on this topic:
Programmatically navigate using react router
本文标签: javascriptReactrouter browserHistorypush doesn39t redirectStack Overflow
版权声明:本文标题:javascript - React-router browserHistory.push doesn't redirect - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741491421a2381635.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论