Programmatically navigate using react router

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

    To use withRouter with a class-based component, try something like this below. Don't forget to change the export statement to use withRouter:

    import { withRouter } from 'react-router-dom'

    class YourClass extends React.Component {
      yourFunction = () => {
        doSomeAsyncAction(() =>
          this.props.history.push('/other_location')
        )
      }
    
      render() {
        return (
          
    ) } } export default withRouter(YourClass);

提交回复
热议问题