Programmatically navigate using react router

后端 未结 30 2370
無奈伤痛
無奈伤痛 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:45

    The right answer was for me at the time of writing

    this.context.router.history.push('/');
    

    But you need to add PropTypes to your component

    Header.contextTypes = {
      router: PropTypes.object.isRequired
    }
    export default Header;
    

    Don't forget to import PropTypes

    import PropTypes from 'prop-types';
    

提交回复
热议问题