简体   繁体   English

React-Native 无限循环

[英]React-Native infinite loop

I am trying to get data from my firebase-firestore I an showing a loading state to wait for the data to load however when it does load it keeps returning the firestore data infinite times.我正在尝试从我的 firebase firebase-firestore获取数据,并显示正在加载 state 以等待数据加载,但是当它确实加载时,它会无限次返回 firestore 数据。 Please may someone help me.请有人帮助我。

This is my code Paper is just a custom component这是我的代码Paper只是一个自定义组件

import Paper from '../Components/Paper'
import firebase from 'firebase'
import { useState } from 'react'

const Home = (props) => {
    const renderMealItem = (itemData) =>{
        return (
            <Paper 
                title={itemData.item.name}
                serves={itemData.item.servings}
                time={itemData.item.time}
                image={itemData.item.imageUri}

            />
        )
    }
    const [loading, setLoading] = useState(false)
    const [all, setAll] = useState([])
    
    useEffect(() => {
        setLoading(true)
        checkReturn()
        getUser()
        
    },[])

    const checkReturn = () => {
        if(all !== undefined){
            setLoading(false)
        }
    }

    const getUser = async() => {
        try {
            await firebase.firestore()
            .collection('Home')
            .get()
                .then(querySnapshot => {
                    querySnapshot.docs.forEach(doc => {
                        setAll(JSON.stringify(doc.data()));
                    });
                });
                    
        }catch(err){
            console.log(err)
        }
        
    }
    return(
        <View style={styles.flatContainer}>
            <FlatList
                data={all}
                keyExtractor={(item, index) => index.toString()}
                renderItem={renderMealItem}/> 
        </View>
    )
}

Try passing an empty array as an argument to useEffect like so:尝试将一个空数组作为参数传递给useEffect ,如下所示:

   useEffect(() => {
        setLoading(true)
        checkReturn()
        getUser()
        
    }, [])

useEffect without second parameter will get executes on each update.没有第二个参数的useEffect将在每次更新时执行。

    useEffect(() => {
        setLoading(true)
        checkReturn()
        getUser()
        
    })

so this will set the loading and tries to get the user.所以这将设置加载并尝试获取用户。 and when the data comess from server, it will get runned again.当数据来自服务器时,它将再次运行。

So you should change it to: useEffect(() => {...}, []) to only get executed on mount phase(start).所以你应该把它改成: useEffect(() => {...}, [])只在挂载阶段(开始)执行。

Update: you should check for return on every update, not only at start.更新:您应该检查每次更新的回报,而不仅仅是在开始时。 so change the code to:所以将代码更改为:

    useEffect(() => {
        setLoading(true)
        getUser()  
    }, [])
    useEffect(() => {
        checkReturn()
    })

Ps: there is another issue with your code as well: Ps:您的代码还有另一个问题:

querySnapshot.docs.forEach(doc => {
  setAll(JSON.stringify(doc.data()));
});

maybe it should be like:也许它应该是这样的:

setAll(querySnapshot.docs.map(doc => JSON.stringify(doc.data())));

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

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