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 */ /> ); }