2016-03-31 28 views
6

我試圖以編程方式導航到像這樣this.props.history.push('/new-path');它的工作不同的頁面,但我得到了在控制檯中棄用警告說:this.props.history被棄用(反應路由器)

Warning: [react-router] props.history and context.history are deprecated. Please use context.router.更多關於它在這裏https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#changes-to-thiscontext


後來我試圖用像這樣this.context.router.push('/new-path')這種新方法,但這似乎並沒有是正確的做法。

+0

可能相關:http://stackoverflow.com/questions/35213446/react-路由器這個上下文路由器 - 推不 - 不重新掛載-的分量 –

回答

13

也許你忘了將路由器定義爲contextType?假設你有一個像組件:

class YourComponent extends React.Component { 
     render() { 
     return <div></div>; 
     } 
} 

你必須定義contextTypes如下正確的組件下:

YourComponent.contextTypes = { 
    router: React.PropTypes.object.isRequired 
};