Using React Router withRouter

后端 未结 2 2044
忘掉有多难
忘掉有多难 2020-12-10 14:28

How do you get the routing context, location, params, etc, when using withRouter()?

import { withRouter } from \'react-router\';

const Some         


        
2条回答
  •  情歌与酒
    2020-12-10 15:34

    So, no using context anymore. It's all available in the props:

    SomeComponent.propTypes = {
      location: React.PropTypes.shape({
        pathname: React.PropTypes.string,
        query: React.PropTypes.shape({
          ...
        })
      }),
      params: React.PropTypes.shape({
        ...
      }),
      router: React.PropTypes.object
    }
    
    const ComposedWithRouter = withRouter(SomeComponent);
    

    So then lets say in SomeComponent you wanted to send the user to a new route, you simply do this.props.router.push('someRoute')

提交回复
热议问题