在React/Redux中获取Dispatch的价值



我正试图通过twilioapi实现帐户验证,它会向用户发送代码,并将generatedCode的状态设置为该值,但我很难获得调度的值。如果我用axios调用一个简单的await,它工作得很好,但用调度就不那么好了

操作:

export const attemptRegisterVerify = (phone) => async (dispatch) => {
dispatch(registerVerifyBegin());
await postRegisterVerifyPhone(phone)
.then((res) => {
dispatch(registerVerifySuccess(res.data));
})
.catch((error) => {
dispatch(registerVerifyFail(error));
});
};

句柄验证:

const handleVerification = async (values) => {
const { email, phone } = values;
setModal(true);
try {
const response = await axios.post(
'http://localhost:8000/api/v1/auth/register/check-duplicate',
{
email,
phone,
}
);
if (response.data.success) {
switch (response.data.message) {
case 0:
try {
const response = await dispatch(
attemptRegisterVerify({ to: phone })
);
console.log(response);
// if (response.data.success) {
//   setGeneratedCode(response.data.message);
//   console.log(generatedCode);
// } else {
//   console.log(response.data.message);
// }
} catch (error) {
console.log(error);
}
break;
case 1:
console.log('Email Address already in use.');
break;
case 2:
console.log('Phone number already in use.');
break;
case 3:
console.log('Email and Phone in use.');
break;
}
} else {
console.log(response.data.message);
}
} catch (error) {
console.log(error);
}
};

console.log(响应(返回undefined。有没有办法让它等到调度完成后再继续?


export const attemptRegisterVerify = (phone) => async (dispatch) => {
dispatch(registerVerifyBegin());
await postRegisterVerifyPhone(phone)
.then((res) => {
dispatch(registerVerifySuccess(res.data));
return res.data; // return console.log(res.data) works
})
.catch((error) => {
dispatch(registerVerifyFail(error));
});
};

Dispatch返回您在函数调用中返回的内容。在异步thunk的情况下,默认情况下它将返回Promise<void>,除非您返回一些内容。

如果在attemptRegisterVerifythunk中,如果在其末尾添加return 'test',则console.log(response)将记录'test'

在非thunk中,分派的返回值将是生成的操作。

相关内容

  • 没有找到相关文章

最新更新