React Router 4 this.context 未定义错误,导致this.context.router.history无法使用
解决方法:
class App extends React.Component {
constructor(props, context) {
super(props, context);
console.log("context",this.context) //=> object
render(){
return <div>
<Bottom onTouchTap={() => {
this.select(1);
this.context.router.history.push("/home");
}}
>点我</Bottom>
</div>
}
}
App.contextTypes = {
router:React.PropTypes.object
}
export default App;
重点代码: 加了下面才有context,否则 undefined
App.contextTypes = {
router:React.PropTypes.object
}