javascript - Firebase with useeffect cleanup function - Stack Overflow

I'm trying to get all data with the 'public' parameter of a firebase collection and then

I'm trying to get all data with the 'public' parameter of a firebase collection and then use useffect, but the console accuses error.

I took this structure from firebase's documentation:

but the console says 'Warning: Can't perform a React state update on an unmounted ponent. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function'

So I went to this other page:

But I'm not using onSnapshot, besides firebase documentation seems wrong to me, since unsub is not a function.

useEffect(() => {

        let list = []

        const db = firebase.firestore().collection('events')
        let info = db.where('public', '==', 'public').get().then(snapshot => {
            if (snapshot.empty) {
                console.log('No matching documents.');
                setLoading(false)
                return;
            }
            snapshot.forEach(doc => {
                console.log(doc.id, "=>", doc.data())
                list.push({
                    id: doc.id,
                    ...doc.data()
                })
            })
            setEvents(list)
            setLoading(false)
        })
            .catch(error => {
                setLoading(false)
                console.log('error => ', error)
            })
        return () => info

    }, [])

I'm trying to get all data with the 'public' parameter of a firebase collection and then use useffect, but the console accuses error.

I took this structure from firebase's documentation: https://firebase.google./docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection

but the console says 'Warning: Can't perform a React state update on an unmounted ponent. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function'

So I went to this other page: https://firebase.google./docs/firestore/query-data/listen#detach_a_listener

But I'm not using onSnapshot, besides firebase documentation seems wrong to me, since unsub is not a function.

useEffect(() => {

        let list = []

        const db = firebase.firestore().collection('events')
        let info = db.where('public', '==', 'public').get().then(snapshot => {
            if (snapshot.empty) {
                console.log('No matching documents.');
                setLoading(false)
                return;
            }
            snapshot.forEach(doc => {
                console.log(doc.id, "=>", doc.data())
                list.push({
                    id: doc.id,
                    ...doc.data()
                })
            })
            setEvents(list)
            setLoading(false)
        })
            .catch(error => {
                setLoading(false)
                console.log('error => ', error)
            })
        return () => info

    }, [])
Share Improve this question asked Oct 19, 2019 at 13:44 user12243148user12243148 3
  • Can you try return () => info() – SuleymanSah Commented Oct 19, 2019 at 14:00
  • then it says "TypeError: info is not a function" – user12243148 Commented Oct 19, 2019 at 14:19
  • Did you check these posts? stackoverflow./questions/56894293/… – SuleymanSah Commented Oct 19, 2019 at 14:21
Add a ment  | 

2 Answers 2

Reset to default 4

You need to invoke the listener function to detach and unmount. https://firebase.google./docs/firestore/query-data/listen#detach_a_listener

useEffect(() => {

        let list = []

        const db = firebase.firestore().collection('events')
        let info = ...
        return () => info() # invoke to detach listener and unmount with hook

    }, [])

You don't need to clear a .get(), it's like a normal Fetch api call.

You need to make sure your ponent is still mounted when you setLoading

Example:

// import useRef
import { useRef } from 'react'
// At start of your ponent
const MyComponent = () =>{
   const isMounted = useRef(false)
   useEffect(() => {
     isMounted.current = true;
     return () => isMounted.current = false;
  }, [])

   // Your query
   db.where('public', '==', 'public').get().then(snapshot => {
      if(isMounted.current){
        // set your state here. 
      }
   })

  return <div></div>
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745131725a4613004.html

相关推荐

  • javascript - Firebase with useeffect cleanup function - Stack Overflow

    I'm trying to get all data with the 'public' parameter of a firebase collection and then

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信