useEffect cleanup funtion

Warning: Can't perform a React state update on an unmounted component. 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.
๊ฒฝ๊ณ  : ๋งˆ์šดํŠธ ํ•ด์ œ ๋œ ๊ตฌ์„ฑ ์š”์†Œ์—์„œ ๋ฐ˜์‘ ์ƒํƒœ ์—…๋ฐ์ดํŠธ๋ฅผ ์ˆ˜ํ–‰ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ž‘๋™ํ•˜์ง€ ์•Š์ง€๋งŒ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ˆ˜์ •ํ•˜๋ ค๋ฉด useEffect ์ •๋ฆฌ ๊ธฐ๋Šฅ์—์„œ ๋ชจ๋“  ๊ตฌ๋… ๋ฐ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ทจ์†Œํ•˜์‹ญ์‹œ์˜ค.
useEffect(() => { axios.post("/api/like/get-like-state", variable).then((res) => { if (res.data.success && res.data.liked) { setLiked(res.data.liked); } }); }, [variable]);
useEffect๊ฐ€ ์‹คํ–‰๋˜๋Š” ์ตœ์ ์˜ ์žฅ์ˆ˜์— ๋”ฐ๋ผ ๋งˆ์šดํŠธ๋œ ๊ฐ’์ด true๋กœ ์„ค์ •๋˜๋ฉด ์ •๋ฆฌ ๊ธฐ๋Šฅ์ด false๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ๋‚ฑ๊ฐœ ๊ตฌ์„ฑํ’ˆ์ด ummounted์™€ ๋งˆ์šดํŠธ๋œ ๊ฐ’์ด false์ผ ๋‚ด์—ญ์ด ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.mounted๊ฐ€ ํƒ‘์žฌ๋œ ๊ฐ’์ด true(๋น„๋™๊ธฐ) ๋‚ ์งœ ์—…๋ฐ์ดํŠธ๊ฐ€ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.
useEffect(() => { let mounted = true; // ์ข‹์•„์š” ์ƒํƒœ ๊ฐ€์ ธ์˜ค๊ธฐ axios.post("/api/like/get-like-state", variable).then((res) => { if (res.data.success && res.data.liked && mounted) { setLiked(res.data.liked); } }); // clean-up return () => (mounted = false); }, [variable]);
const Example = (props) => { const unmounted = useRef(false); useEffect(() => { return () => { unmounted.current = true } }, []); const setFilter = () => { // ... props.dispatch(fetchCourses()).then(() => { if (!unmounted.current) { setLoading(false); } }) } // ... return ( <ReactTable onFetchData={setFilter} /* other props omitted */ /> ); }