Programmatically navigate using react router

后端 未结 30 2302
無奈伤痛
無奈伤痛 2020-11-21 05:18

With react-router I can use the Link element to create links which are natively handled by react router.

I see internally it calls t

30条回答
  •  感动是毒
    2020-11-21 05:49

    Here's how you do this with react-router v2.0.0 with ES6. react-router has moved away from mixins.

    import React from 'react';
    
    export default class MyComponent extends React.Component {
      navigateToPage = () => {
        this.context.router.push('/my-route')
      };
    
      render() {
        return (
          
        );
      }
    }
    
    MyComponent.contextTypes = {
      router: React.PropTypes.object.isRequired
    }
    

提交回复
热议问题