본문 바로가기
Q&A해결2025년 1월 8일1분 읽기

React useEffect 클린업 함수에서 abort controller가 작동하지 않아 메모리 누수 발생

주니어개발자소연
조회 589댓글 4

React 컴포넌트에서 API 호출을 하고 있는데, 컴포넌트가 unmount 되어도 fetch가 계속 진행됩니다. AbortController를 사용했는데도 콘솔에 "Can't perform a React state update on an unmounted component" 경고가 나옵니다.

useEffect(() => {
  const controller = new AbortController()
  fetch("/api/data", { signal: controller.signal })
    .then(res => res.json())
    .then(data => setData(data))
  return () => controller.abort()
}, [])

댓글 0

아직 댓글이 없습니다.
Ctrl+Enter로 등록