最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - this.props.history is deprecated (react-router) - Stack Overflow

programmeradmin2浏览0评论

I was trying to programatically navigate to a different page like so this.props.history.push('/new-path'); it worked, but I got a deprecation warning in console saying:

Warning: [react-router] props.history and context.history are deprecated. Please use context.router. more about it here .0.0.md#changes-to-thiscontext


Afterwards I tried to use this new method like so this.context.router.push('/new-path') but this doesn't seem to be the right approach.

I was trying to programatically navigate to a different page like so this.props.history.push('/new-path'); it worked, but I got a deprecation warning in console saying:

Warning: [react-router] props.history and context.history are deprecated. Please use context.router. more about it here https://github./reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#changes-to-thiscontext


Afterwards I tried to use this new method like so this.context.router.push('/new-path') but this doesn't seem to be the right approach.

Share Improve this question edited May 7, 2016 at 4:43 Joshua Dance 10.5k4 gold badges79 silver badges83 bronze badges asked Mar 31, 2016 at 10:20 IljaIlja 46.5k103 gold badges289 silver badges527 bronze badges 1
  • Possibly related: stackoverflow./questions/35213446/… – James Donnelly Commented Mar 31, 2016 at 10:24
Add a ment  | 

1 Answer 1

Reset to default 13

Maybe you did forget to define the router as contextType? Assuming you have a ponent like:

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

You have to define the contextTypes as follows right beneath your ponent:

YourComponent.contextTypes = {
  router: React.PropTypes.object.isRequired
};
发布评论

评论列表(0)

  1. 暂无评论