Automatic redirect after login with react-router

后端 未结 7 2548
广开言路
广开言路 2020-12-01 07:17

I wanted to build a Facebook login into my react/react-router/flux application. I have a listener registered on the login event and would like to redirect t

7条回答
  •  春和景丽
    2020-12-01 07:41

    React Router v3

    This is what I do

    var Router = require('react-router');
    Router.browserHistory.push('/somepath');
    

    React Router v4

    Now we can use the component in React Router v4.

    Rendering a will navigate to a new location. The new location will override the current location in the history stack, like server-side redirects.

    import React, { Component } from 'react';
    import { Redirect } from 'react-router';
    export default class LoginComponent extends Component {
        render(){
            if(this.state.isLoggedIn === true){
                return ();
            }else{
                return (
    Login Please
    ); } } }

    Documentation https://reacttraining.com/react-router/web/api/Redirect

提交回复
热议问题