React Native 从 firestore 获取数据然后分派不工作-分派空数组

React Native fecth data from firestore then dispatch not working-Dispatch empty array

我正在尝试开发我的第一个 React Native 应用程序,但我被来自 firestore 的 store/fetching 数据吓坏了。

问题是在从 firestore 获取所有数据之前进行分派,所以我的 filteredProfiles 数组是空的,我屏幕上的平面列表也是空的。

我应该修改什么?如果有人能帮助我,我将不胜感激..谢谢!!

这是我屏幕上的代码:

ProfilesListScreen.js

const ProfilesListScreen = props => {   
  const connectedUser = useSelector(state => state.profiles.user); 
  const filteredProfiles = useSelector(state => state.profiles.filteredProfiles)

    const dispatch = useDispatch();

    useEffect(() => {
      dispatch(fetchProfiles()); 
      }, [dispatch]);

我的 profilesAction.js 在我的店里:

export const fetchProfiles= () => {

    
    const loadedProfiles = [ ];

    return async dispatch => {               
        

        await firebase.firestore().collection('users')
        .get()
        .then((profileSnapShot) => {            
            profileSnapShot.forEach((doc) => {               
                const firstname=  doc.get("firstname");
                const birth = doc.get("birth");
                const age = getAge(birth.seconds);
                const sex = doc.get("sex");                 
                const newProfile= new profile(doc.id, firstname,age, "https://www.google.fr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
                
                loadedProfiles.push(newProfile); 
               
                
            }); 
        })

         dispatch({ type: SET_PROFILESLIST, profilesList: loadedProfiles } ) ;
        

    };

和我的减速机profiles.js

case SET_PROFILESLIST:
          const newProfilesList = action.profilesList;
           return { 
            filteredProfiles : newProfilesList,
           };

尝试更新 fetchProfiles 的代码,如下所示,这可能会帮助您解决问题。

export const fetchProfiles= () => {
    const loadedProfiles = [];
    return async dispatch => {               
        const profileSnapShot = await firebase.firestore().collection('users').get()
        profileSnapShot.forEach((doc) => {               
            const firstname=  doc.get("firstname");
            const birth = doc.get("birth");
            const age = getAge(birth.seconds);
            const sex = doc.get("sex");                 
            const newProfile= new profile(doc.id, firstname,age, "https://www.google.fr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
            loadedProfiles.push(newProfile); 
        });
        dispatch({ type: SET_PROFILESLIST, profilesList: loadedProfiles });
    }
};