简体   繁体   中英

How to clean up event listener inside useCallback

const ref = useCallback((node) => {
 if (node) {
  // ...intersection observer code goes here
 }
}, []);

return <div ref={ref} />

I use useCallback hook to setup intersection observer listener once ref 's node is available. My question is what is the correct way to kill intersection observer when component unmounts?

move your listeners to useEffect hook

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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