react router - Redirection after login

后端 未结 7 2207
天命终不由人
天命终不由人 2021-01-30 14:22

Could you please help me in understanding the redirection mechanism I could use with latest version of react router ( v1.1.0 ) . I would like to redirect to a

7条回答
  •  时光说笑
    2021-01-30 14:41

    i want just share the actual answer at 2020 year. The main way for storing previous location in state is the same. But onEnter was removed from library. Now we can use AuthRoute as in the documentation:

    
      
    
    
      
    
    
    const AuthRoute = ({ children, isAuthorized, ...rest }) => {
      const loginLink = usePrepareLink({
        to: "/login",
        isRelativePath: true
      });
    
      return (
        
          isAuthorized ? (
            children
          ) : (
            
          )
        } />
      );
    };
    

    and we can use the state to restore previouse URL after login

    const onSignIn = useCallback(() => {
      setIsAuthorized(value);
      const link = (state && state.from) || "/restore-prevented-route";
      history.replace(link);
    }, [setIsAuthorized, value, history, state]);
    

    The details you can find here (or RU)

提交回复
热议问题