Bra*_*rad 3 javascript ecmascript-6 reactjs react-router
是否有更简单的方法Router从组件访问对象来执行调用transitionTo()而不使用Navigation mixin?这是ES6组件.目前在按钮点击等事件上,我一直在写这样的事情:
class Button extends React.Component {
handleClick(e) {
e.preventDefault();
var router = this._reactInternalInstance._context.router;
router.transitionTo('/search');
}
render() {
return (
<button onClick={this.handleClick.bind(this)}>
{this.props.children}
</button>
);
}
}
Run Code Online (Sandbox Code Playgroud)
Per Mat Gessel的评论,在构造函数中添加上下文作为参数将使您可以访问路由器.
class Button extends React.Component {
constructor(props, context) {
super(props, context);
this.context = context;
}
handleClick(e) {
e.preventDefault();
this.context.router.transitionTo('/search');
}
render() {
return (
<button onClick={this.handleClick.bind(this)}>
{this.props.children}
</button>
);
}
}
Button.contextTypes = {
router: React.PropTypes.object.isRequired
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2764 次 |
| 最近记录: |