6

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.com/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.

Joshua Dance
  • 8,847
  • 4
  • 67
  • 72
Ilja
  • 44,142
  • 92
  • 275
  • 498
  • Possibly related: http://stackoverflow.com/questions/35213446/react-router-this-context-router-push-does-not-remount-the-component – James Donnelly Mar 31 '16 at 10:24

1 Answers1

13

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

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

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

YourComponent.contextTypes = {
  router: React.PropTypes.object.isRequired
};
Richard Rutsche
  • 1,156
  • 8
  • 11