React Component Lifecycle

LOOPY·2021년 8월 23일

출처 https://ssangq.netlify.app/posts/react-lifecycle

  • React Component는 탄생부터 죽음까지 여러 지점에서 개발자가 작업이 가능하도록 method를 overriding 할 수 있게 해줌

1. 최초 출력: Initialization(생성자) & Mounting(최초 render)

  • constructor -> componentWillMount -> render -> componentDidMount

2. props, state 변경: Updation

  • componentWillReceiveProps -> shouldComponentUpdate -> componentWillUpdate -> render -> componentDidUpdate
    1) componentWillReceiveProps: props나 state 둘 중 하나만 변경되어도 newProps와 newState를 인자로 호출됨
    • return type은 boolean으로 true면 render 호출, false면 render 호출X
    • 구현하지 않으면 default는 true
      2) componentWillUpdate: 컴포넌트 재 랜더링 직전에 호출됨
    • setState 등 호출 불가능
      3) componentDidUpdate: 컴포넌트가 재 랜더링 마치면 호출됨

3. Unmounting

  • componentWillUnmount

4. 16.3 이후 최신버전

출처 https://www.zerocho.com/category/React/post/579b5ec26958781500ed9955
(진한 글씨가 변경된 부분)

  • constructor –> getDerivedStateFromProps –> render –> componentDidMount
  • getDerivedStateFromProps –> shouldComponentUpdate –> render –> getSnapshotBeforeUpdate –> componentDidUpdate
  • componentWillUnmount

+) componentDidCatch: 문제 발생 시 화면 멈추지 않고 err 페이지 출력 가능

profile
2년차 프론트엔드 개발자의 소소한 기록을 담습니다 :-)

0개의 댓글