本文介绍了如何使用钩子在 react-native 中分派多个动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从下面的代码中,我试图分派两个操作,这些操作从 Firestore 数据库用户集合中获取登录用户的注册姓名和姓氏,并更新我的应用程序中的姓名和姓氏状态.然而,由于某种原因,只有第二个动作被调度.我的应用中的姓氏状态已更新,但姓名状态仍为空.

我尝试使用异步函数来调用调度,但仍然没有.我试过将调度代码移动到代码的不同部分,但仍然没有.无论我把代码放在哪里,都只会调用第二个调度.到目前为止似乎没有任何工作.我似乎不能让两个调度只工作一个.附带说明一下,我没有使用 redux,或者任何中间件只是使用 useReducer 和 useContext 来响应钩子.

firebase.auth().onAuthStateChanged(user => {如果(用户){const userId = user.uid;常量数据库 =firebase.firestore().collection('users').doc(userId);db.get().then(userId => {如果(用户 ID){派遣({类型:'fill_name',有效载荷:userId.data().Name})派遣({类型:'fill_surname',有效载荷:userId.data().Surname})} 别的 {返回空;}})} 别的 {导航('介绍');}});
解决方案

不能从一个 action 中调度两次的主要原因是 { dispatch } 逻辑首先创建的原因.因为有一个地方包含所有应用程序真相",有一种改变托拉的方式和一座山来获得一个

你可以做的是同时发送名字和姓氏 -

firebase.auth().onAuthStateChanged(user => {如果(用户){const userId = user.uid;常量数据库 =firebase.firestore().collection('users').doc(userId);db.get().then(userId => {如果(用户 ID){派遣({类型:'fill_name',firstName: userId.data().Name姓氏:userId.data().姓氏})} 别的 {返回空;}})} 别的 {导航('介绍');}});

当在减速器中听动作时,它看起来像 -

 action.firstName

对于名字

From the code below, I am trying to dispatch two actions that take the registered name and surname of a logged in user from the firestore database user collection and updates the name and surname state in my app. However, for some reason, only the second action gets dispatched. The surname state in my app gets updated but the name state remains empty.

I've tried using an async function to call the dispatch but still nothing. I've tried moving the dispatch code to different sections of the code and still nothing. No matter where I put the code, only the second dispatch gets called. Nothing seems to be working so far. I can't seem to make two dispatches work only one. As a side note, I am not using redux, or any middleware just react hooks with useReducer and useContext.

firebase.auth().onAuthStateChanged(user => {
    if (user) {
        const userId = user.uid;
        const db =
firebase.firestore().collection('users').doc(userId);
        db.get()
        .then(userId => {
            if (userId) {
                dispatch({
                    type: 'fill_name',
                    payload: userId.data().Name
                })
                dispatch({
                    type: 'fill_surname',
                    payload: userId.data().Surname
                })
            } else {
                return null;
            }
        })
    } else {
        navigate('Intro');
    }
});
解决方案

the reason why you cant dispatch twice from one action is for the main reason why { dispatch } logic created for the first place. for having one place with all the "app-truth" in it, with one way of change the Torah and one mountain to get one

what you can do is dispatch both first and last name like -


firebase.auth().onAuthStateChanged(user => {
    if (user) {
        const userId = user.uid;
        const db =
firebase.firestore().collection('users').doc(userId);
        db.get()
         .then(userId => {
            if (userId) {
                dispatch({
                    type: 'fill_name',
                    firstName: userId.data().Name
                    surName: userId.data().Surname
                })
            } else {
                return null;
            }
        })
    } else {
        navigate('Intro');
    }
});

and when listening in the reducer to the action it gonna look like -

   action.firstName

for first name

这篇关于如何使用钩子在 react-native 中分派多个动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:39