React cleanup

Created
Created
2021 Apr 5 12:44
Tags
Tags

return cleanup function - unmount

return function cleanup() { }
해당 코드는 처음 렌더링 될 때의 state를 출력해서 문제가 있다
마지막 state로 cleanup하려면 React의 re-render와 독립적인 ref의
React useRef()
를사용해야
const Conter = () =>{ const [count, setCount] = useState(0) const countRef = useRef(count) useEffect(() => { return () => { console.log("unmount 시 출력", countRef.current); } }, [])}
 
 
[React] useRef 200% 활용하기
혼자 나름대로 공부한 내용을 바탕으로만 개발하다가, 이번에 면접을 준비하면서 좀 더 깊은 공부를 하고 싶다는 생각에 본 200% 활용하기 포스팅을 작성하게 되었습니다! 최근에 면접 질문으로 아래와 같은 질문을 받았습니다. React에서 Component가 unmount 될 때의 state를 딱 한번만 log를 찍고 싶은데, 그러려면 어떻게 구현하면 될까요?
[React] useRef 200% 활용하기

Recommendations