Programmatically Navigate using react-router

前端 未结 8 1315
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 23:07

I am developing an application in which I check if the user is not loggedIn. I have to display the login form, else dispatch an action

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-21 23:11

    Considering you are using react-router v4

    Use your component with withRouter and use history.push from props to change the route. You need to make use of withRouter only when your component is not receiving the Router props, this may happen in cases when your component is a nested child of a component rendered by the Router and you haven't passed the Router props to it or when the component is not linked to the Router at all and is rendered as a separate component from the Routes.

    import {withRouter} from 'react-router';
    
    class App extends React.Component {
         ...
         componenDidMount() {
            // get isLoggedIn from localStorage or API call
            if (isLoggedIn) {
                 // dispatch an action to change the route
                 this.props.history.push('/home');
            }
         }
         render() {
             // return login component
             return 
        }
    }
    
    
    export default withRouter(App);
    

    Important Note

    If you are using withRouter to prevent updates from being blocked by shouldComponentUpdate, it is important that withRouter wraps the component that implements shouldComponentUpdate. For example, when using Redux:

    // This gets around shouldComponentUpdate

    withRouter(connect(...)(MyComponent))
    

    // This does not

    connect(...)(withRouter(MyComponent))
    

    or you could use Redirect

    import {withRouter} from 'react-router';
    
    class App extends React.Component {
         ...
         render() {
             if(isLoggedIn) {
                  return 
             }
             // return login component
             return 
        }
    }
    

    With react-router v2 or react-router v3, you can make use of context to dynamically change the route like

    class App extends React.Component {
         ...
         render() {
             if (isLoggedIn) {
                 // dispatch an action to change the route
                 this.context.router.push('/home');
             }
             // return login component
             return 
        }
    }
    
    App.contextTypes = {
        router: React.PropTypes.object.isRequired
    }
    export default App;
    

    or use

    import { browserHistory } from 'react-router';
    browserHistory.push('/some/path');
    

提交回复
热议问题