Automatic redirect after login with react-router

后端 未结 7 2469
广开言路
广开言路 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 08:03

    React Router v3

    Navigating inside components

    You should use withRouter decorator when it's necessary to redirect inside a component. The decorator uses context instead of you.

    import {withRouter} from 'react-router'
    
    fucntion Foo(props) {
        props.router.push('/users/16');
    }
    
    export default withRouter(Foo);
    

    withRouter(Component, [options])

    A HoC (higher-order component) that wraps another component to enhance its props with router props.

    withRouterProps = { ...componentProps, router, params, location, routes }

    Pass in your component and it will return the wrapped component.

    You can explicit specify router as a prop to the wrapper component to override the router object from context.

提交回复
热议问题