#Is using both observer.unobserve and observer.disconnect redundant

1 messages · Page 1 of 1 (latest)

jolly trellis
#

I'm using the IntersectionObserver in a useEffect. This is the clean up function for my useEffect. Is using both observer.unobserve and observer.disconnect redundant? Should I only use one?

return () => {                        
    items.forEach((item) => {
        observer.unobserve(item)
    })
                                                            
    observer.disconnect()
                        
}, [])
small lagoon
#

the doc is pretty clear about it

#

use unobserve to stop watching 1 element (but keep watching the other elements)

#

use disconnect to stop watching at all

#

so you need to use disconnect, since it's kind of the "destructor" method, exactly what you need in a cleanup function