Navigating Programmatically in React-Router v4

后端 未结 10 1182
后悔当初
后悔当初 2020-12-02 11:16

I couldn\'t wait and I jumped into using the latest alpha version of react-router v4. The all-new is great in keeping your U

10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 11:51

    use withRouter:

    import React, { PropTypes } from 'react'
    import { withRouter } from 'react-router'
    
    // A simple component that shows the pathname of the current location
    class ShowTheLocation extends React.Component {
      static propTypes = {
        match: PropTypes.object.isRequired,
        location: PropTypes.object.isRequired,
        history: PropTypes.object.isRequired
      }
    
      render() {
        const { match, location, history } = this.props
    
        return (
          
    You are now at {location.pathname}
    ) } } // Create a new component that is "connected" (to borrow redux // terminology) to the router. const ShowTheLocationWithRouter = withRouter(ShowTheLocation)

提交回复
热议问题