componentWillUnmount()_React

miin·2021년 10월 16일
0

React

목록 보기
18/52
post-thumbnail

componentWillUnmount()

  • 컴포넌트가 마운트 해제되어 제거되기 직전에 호출된다
  • 이 메서드 내에서 타이머제거, 네트워크 요청취소, DidMount내에서 생성된 구독 해제 등 필요한 모든 정리 작업 수행
  • 이제 컴포넌트는 다시 렌더링 되지 않으므로, setState를 호출하면 안된다.
    컴포넌트 인스턴스가 마운트 해제되고 나면, 절대로 다시 마운트 되지 않음

사용하기

detectScroll = () => {
    this.setState({
      scrollY: window.scrollY,
    });
  };
  componentDidMount() {
    window.addEventListener('scroll', this.detectScroll);
  }
  componentWillUnmount() {
    window.removeEventListener('scroll', this.detectScroll);
  }

0개의 댓글