输入用户正确的电子邮件和密码后,Reducer返回包装在Promise中的SIGNED_IN值。我很困惑为什么它不简单地返回'SIGNED_IN':true?
authentication: Promise
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Object
SIGNED_IN: true
我正在调度此动作:
const mapDispatchToProps = dispatch => ({
authUser: () => dispatch(authUser('richard@tiger.com', 'xzy98765'))
})
动作创建者:
export const authUser = (email, password) => async dispatch => {
console.log('asd')
try {
const user = await Auth.signIn(email, password)
if (user.challengeName === 'MFA_SETUP') {
// This happens when the MFA method is TOTP
// The user needs to setup the TOTP before using it
// More info please check the Enabling MFA part
//Auth.setupTOTP(user);
} else {
// The user directly signs in
console.log(user)
return dispatch({type:'SIGN_IN_USER_SUCCESS'})
}
} catch (e) {
console.log(e)
return dispatch({type:'SIGN_IN_USER_FAIL'})
}
}
减速器:
export default async (state, action) => {
console.log('authReducer state',state)
switch (action.type) {
case 'SIGN_IN_USER_SUCCESS':
return {
...state,
SIGNED_IN: true
}
case 'SIGN_IN_USER_FAIL':
return {
...state,
SIGNED_IN: false
}
}
}
答案 0 :(得分:1)
您的reducer不应是异步的(异步函数总是返回诺言)。就是这样:
export default function (state, action)
您的动作是异步的,因为它们正在等待响应,而reducer只是一个正常的函数,它接受异步动作中的值。