![](/img/trans.png)
[英]Why is my redux app not caching an async api call in redux-thunk?
[英]UseEffect not fired when redux props change caused by async api call with redux-thunk
我有一個與 redux 連接的功能登錄頁面,我正在觸發一個異步事件 onSubmit 將觸發emailLogin
操作,我正在使用useEffect
來檢測isLoading
道具的更改以查看登錄是否完成。 如果登錄成功,redux store 應該有 user 對象,如果登錄失敗,user 應該保持為 null。
問題是,我知道登錄成功,應該觸發isLoading
的更改,決定是否使用useEffect
的參數,但是, useEffect
沒有被觸發。 另外, console.log('done');
在await emailLogin(authData);
永遠不會被解雇。 有什么不對。
import React, { useState, useEffect } from 'react';
import { connect } from 'react-redux';
import { Link, useHistory } from 'react-router-dom';
import { emailLogin } from '../actions/index';
function Login({ user, isLoading, emailLogin }) {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const history = useHistory();
useEffect(() => {
console.log('useEffect fired', user, isLoading); //<-----This does not fire after login success
if (user) {
history.push('/protected_home');
}
}, [isLoading]);
const submitEmailLoginForm = async (e) => {
e.preventDefault();
const authData = { email, password };
await emailLogin(authData);
console.log('done'); // <------- This is never fired
};
return (
<div>
<h2>Login</h2>
<Link to="/">back</Link>
<form onSubmit={submitEmailLoginForm}>
<label>
email:
<input
type="text"
name="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</label>
<label>
password:
<input
type="text"
name="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</label>
<input type="submit" value="Submit" />
</form>
</div>
);
}
const mapStateToProps = (state) => ({
user: state.user,
isLoading: state.isLoading
});
const mapDispatch = {
emailLogin: emailLogin
};
export default connect(mapStateToProps, mapDispatch)(Login);
我的操作文件:
import axios from 'axios';
export const authActions = {
EMAIL_LOGIN_START: '@@EMAIL_LOGIN_START',
EMAIL_LOGIN_SUCCESS: '@@EMAIL_LOGIN_SUCCESS'
};
export const emailLogin = ({ email, password }) => async (dispatch) => {
dispatch({ type: authActions.EMAIL_LOGIN_START });
try {
const response = await axios.post('http://localhost:5001/api/auth', {
email: email,
password: password
});
dispatch({
type: authActions.EMAIL_LOGIN_SUCCESS,
payload: {
user: { ...response.data }
}
});
} catch (error) {
console.log('Should dispatch api error', error.response);
}
};
我的減速機:
import { authActions } from '../actions/index';
const initialState = {
user: null,
isLoading: false
};
const userReducer = (state = initialState, action) => {
switch (action.type) {
case authActions.EMAIL_LOGIN_START:
return { ...state, isLoading: true };
case authActions.EMAIL_LOGIN_SUCCESS:
console.log('Reducer check => Login is success'); //<-----this line is printed
return { ...state, user: action.payload.user, isLoading: false };
default:
return state;
}
};
export default userReducer;
在減速器中,我看到成功操作實際上是通過檢查console.log()
觸發的。 同樣在 redux 開發工具中,我實際上可以看到登錄成功並且isLoading
道具已更改:
這解決了我的問題
const mapStateToProps = (state) => ({
user: state.userReducer.user,
isLoading: state.userReducer.isLoading
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.