第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

登錄后響應(yīng)重定向(調(diào)度)

登錄后響應(yīng)重定向(調(diào)度)

寶慕林4294392 2023-07-06 17:50:17
在此類項目中成功登錄后如何將我的網(wǎng)站重定向到主頁:https://github.com/bradtraversy/mern_shopping_list操作 ts 文件 [actions/authActions.ts]:import axios from 'axios';import { returnErrors } from './errorActions';import {  USER_LOADED,  USER_LOADING,  AUTH_ERROR,  LOGIN_SUCCESS,  LOGIN_FAIL,  LOGOUT_SUCCESS,  REGISTER_SUCCESS,  REGISTER_FAIL} from './types';import { IAuthFunction, IConfigHeaders } from '../../types/interfaces';import { useHistory } from 'react-router-dom';//// Register Userexport const register = ({ name, email, password }: IAuthFunction) => (  dispatch: Function) => {  // Headers  const config = {    headers: {      'Content-Type': 'application/json'    }  };  // Request body  const body = JSON.stringify({ name, email, password });  axios    .post('/api/auth/register', body, config)    .then(res =>      dispatch({        type: REGISTER_SUCCESS,        payload: res.data      })    )    .catch(err => {      dispatch(        returnErrors(err.response.data, err.response.status, 'REGISTER_FAIL')      );      dispatch({        type: REGISTER_FAIL      });    });};// Login Userexport const login = ({ email, password }: IAuthFunction) => (  dispatch: Function) => {   // Headers  const config = {    headers: {      'Content-Type': 'application/json'    }  };  // Request body  const body = JSON.stringify({ email, password });  const history = useHistory();// <---------- ERROR HERE  axios    .post('/api/auth/login', body, config)    .then(res => {        dispatch({          type: LOGIN_SUCCESS,          payload: res.data        });        // const history = useHistory(); <----- tried placing it here too        // history.push("/dd");      }    )    .catch(err => {      dispatch(        console.log(err)        // returnErrors(err.response.data, err.response.status, 'LOGIN_FAIL')      );      dispatch({        type: LOGIN_FAIL      });    });};catch 錯誤返回“錯誤:無效的鉤子調(diào)用。鉤子只能在函數(shù)組件的主體內(nèi)部調(diào)用...”
查看完整描述

1 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗 獲得超5個贊

UseEffect()像這樣使用:


import { useHistory } from 'react-router-dom';

//

const LoginForm = ({

    isAuthenticated,

    redirectTo, // <-- here

    error,

    login,

    clearErrors

}: ILoginForm) => {

    const history = useHistory();

    //

    useEffect(() => {

      if(redirectTo)

        history.push(redirectTo);

    });

}


const mapStateToProps = (state: IAuthReduxProps) => ({

    isAuthenticated: state.auth.isAuthenticated,

    redirectTo: state.auth.redirectTo, // <-- here

    error: state.error

});


export default connect(mapStateToProps, { login, clearErrors })(LoginForm);

接口.ts:


export interface IAuthReduxProps {

  auth: { isAuthenticated: boolean, redirectTo: string }; // <-- here added redirectTo

  error: IError;

}

authReducer.ts:


case REGISTER_SUCCESS:

  localStorage.setItem('token', action.payload.token);

  return {

    ...state,

    ...action.payload,

    isAuthenticated: true,

    isLoading: false,

    redirectTo: "/dd" // <-- here

  };


查看完整回答
反對 回復(fù) 2023-07-06
  • 1 回答
  • 0 關(guān)注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號