Javascript 如何在Action Creator Firebase函数上添加异步等待
我正在尝试使用React Hook action creator实现Firebase注册功能。只有当我放入调试器时,代码才能正常工作,所以我认为sync/async进程肯定有问题,只是我不太明白应该将async/await放在哪里 authUtils.js//动作创建者Javascript 如何在Action Creator Firebase函数上添加异步等待,javascript,async-await,firebase-authentication,react-hooks,Javascript,Async Await,Firebase Authentication,React Hooks,我正在尝试使用React Hook action creator实现Firebase注册功能。只有当我放入调试器时,代码才能正常工作,所以我认为sync/async进程肯定有问题,只是我不太明白应该将async/await放在哪里 authUtils.js//动作创建者 export const signup = (firebase, email, password) => async (dispatch) => { try { firebase
export const signup = (firebase, email, password) => async (dispatch) => {
try {
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then((dataBeforeEmail) => {
firebase.auth().onAuthStateChanged((user) => {
user.sendEmailVerification();
});
})
.then((dataAfterEmail) => {
firebase.auth().onAuthStateChanged((user) => {
if (user.emailVerified) {
// Email is verified
dispatch({
type: SIGNUP_SUCCESS,
payload:
"Your account was successfully created! Now you need to verify your e-mail address, please go check your inbox.",
});
} else {
// Email is not verified
dispatch({
type: SIGNUP_ERROR,
payload: "Something went wrong, we couldn't create your account. Please try again.",
});
}
});
})
.catch((error) => {
dispatch({
type: SIGNUP_ERROR,
payload: "Something went wrong, we couldn't create your account. Please try again.",
});
});
} catch (err) {
dispatch({
type: SIGNUP_ERROR,
payload: "Something went wrong, we couldn't create your account. Please try again.",
});
}
};
SignUp.js//反应组件
const SignUp = () => {
const dispatch = useDispatch();
const firebase = useFirebase();
const handleSubmit = (e, formData, inputs) => {
const { email, password } = values;
setIsSubmitting(true);
debugger // The code works when I sequentially debug the code.
dispatch(signup(firebase, email, password));
};
}
另一件事,我使用react redux firebase提供的useFirebase,并使用HOC将firebase作为提供程序传递下去。我认为通过将Firebase实例作为参数从组件传递给action creator函数,我没有正确地使用它。正确的方法是什么?如何直接从action creator访问Firebase实例?既然您的函数是异步的,为什么不在其中使用Wait来大幅简化承诺的使用?