简体   繁体   English

React Native Firebase - State 首次加载时无法检索

[英]React Native Firebase - State Not Able to be Retrieved on First Load

I'm using useEffect to retrieve some user and group data on initial screen load on a react-native app.我正在使用useEffectreact-native应用程序的初始屏幕加载时检索一些用户和组数据。 The following code for this is here:以下代码在这里:

const [groupInfo, setGroupInfo] = useState([]);
    //Called on INITIAL rendering
useEffect(() => {

    async function getGroupData() {

        let groupCode = '';

        //Retrieve group code from user
        await getDoc(doc(db, 'users', email)).then(userSnapshot => {

            if (userSnapshot.exists()) {
                groupCode = userSnapshot.data()['group_code'];

            }
            else { console.log('No user with that email exists!'); }
        }).catch(err => {
            console.log(err);
        });

        //Retrieve group information from user
        await getDoc(doc(db, 'groups', groupCode)).then(groupSnapshot => {

            if (groupSnapshot.exists()) {
                setGroupInfo(groupSnapshot.data());
            }
            else { console.log('No group with that code exists!'); }
        }).catch(err => {
            console.log(err);
        });
    }

    getGroupData();

}, [email]);

The problem is that when I've tried to render this on my return statement, I get an error.问题是当我试图在我的return语句中呈现它时,我得到了一个错误。 I've logged my data before and that has worked fine but it seems that the app loads the view first.我之前已经记录了我的数据并且工作正常但应用程序似乎首先加载了视图。 THe following react render code and error are below:以下反应渲染代码和错误如下:

    return (
    <View style={styles.container}>
        <View style={styles.header}>
            <Text style={styles.headerLeft}>Goals</Text>
            <Text style={styles.headerRight}>Week of 11/20/22</Text>
        </View>
        <Text style={styles.prize}>Prize: Winner gets a free starbucks drink!</Text>
        {/*<View style={styles.goalBox}>
            <Text>Workout 3x per week</Text>
            <CheckBox style={styles.checkbox}/>
        </View> */}

        {/* TODO edit prize screen */}
        <Button title="Edit prize" />


        <View style={styles.memberHeader}>
            <Text style={styles.headerLeft}>Members</Text>
            <Text style={styles.numMembers}>4</Text>
        </View>

        {
            groupInfo['members'].map((memberName, index) =>
                <View style={styles.member}>
                    <Text style={styles.memberText}>{memberName}</Text>
                </View>
            )
        }
        
        <Button title="Invite member" />
    </View>
);

Error:错误:

ERROR  TypeError: Cannot read property 'map' of undefined

This error is located at:
    in ScreenViewGroup (created by SceneView)

EDIT:编辑:

So I think the problem is that at the same time the component is being rendered, the data is being loaded in. I'm still receiving the same error but I've noticed that when I edit my code, the data gets loaded in automatically.所以我认为问题是在渲染组件的同时,正在加载数据。我仍然收到相同的错误,但我注意到当我编辑代码时,数据会自动加载.

Text strings also seem to render in properly as well but just not the array.文本字符串似乎也可以正确呈现,但不是数组。

So I found a work-around.所以我找到了解决方法。 By declaring my initial array with values, React will fill those values in as default values and immediately change them to the firebase values when they load.通过用值声明我的初始数组,React 会将这些值填充为默认值,并在加载时立即将它们更改为 firebase 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM