【发布时间】:2019-07-17 21:27:42
【问题描述】:
在 App.js 中,我管理路由(在下方)。在这两个课程中我都有
import {browserHistory} from "react-router";
当我单击图像时,控制台显示错误无法读取未定义的属性“推送”。所以我的道具不包括历史对象。如何使用 browserHistory 重定向用户?
////
<Router history={browserHistory}>
<div>
<Route path="/profile" component={MyProfile} />
</div
</Router>
/// The code above is in a different class
myprofile(props) {
this.props.history.push('/profile');
}
return (
<React.Fragment>
<img src="../images/my-profile.png" alt="profile" className="myprofile" id="myprofile" onClick={this.myprofile.bind(this)} />
</React.Fragment>
);
【问题讨论】:
-
你试过把组件包裹在
withRouter()吗? -
不需要
this,只需props.history.push -
props的参数myprofile()似乎命名错误。实际上是onClick中的event称为 @Meir -
看来您应该用
<Link/>组件包装您的<img/>标签,而不是尝试使用Router.push()强制导航
标签: javascript reactjs redirect missing-data browser-history