컴포넌트의 수명은 페이지에 렌더링되기 전인 준비과정에서 시작하여 페이지에서 사라질 때 끝난다.
어떤 작업을 업데이트 전후로 작업을 처리하거나 , 불필요한 업데이트 방지를 위해 필요
어떤 작업을 작동하기 전에 실행
어떤 작업을 작동한 후에 실행
모든 라이프사이클은 mount, update, unmount 카테고리로 나눈다
DOM이 생성되고 웹 브라우저상에 나타나는 것
constructor
: 컴포넌트를 새로 만들때마다 호출하는 클래스 생성 methodgetDerivedStateFromProps
: props에 있는 값을 state에 넣을 때 사용render
: 우리가 준비한 UI를 렌더링하느 메서드componentDidMount
: 컴포넌트가 웹 브라우저상에 나타난 후 호출컴포넌트는 다음과 같은 총 네가지 경우에 업데이트된다
props
가 변할 때state
가 변할 때 (setState
)this.forceUpdate
로 강제로 렌더링을 트리거할 때getDerivedStateFromProps
: 마운트과정에서도 호출되며 업데이트가 시작되기 전에도 호출된다. props 변화에 따라 state값에도 변화 주고 싶을 때shouldComponentUpdate
: 컴포넌트가 리렌더링을 할지말지 결정this.forceUpdate( )
함수를 호출하면 이 과정을 생략하고 render 함수를 호출한다.render
: 컴포넌트를 리렌더링getSnapshotBeforeUpdate
: 컴포넌트 변화를 DOM에 반영하기 바로 직전에 호출componentDidUpdate
: 컴포넌트의 업데이트 작업이 끝난 후 호출컴포넌트를 DOM에서 제거하는 것
componentWillUnmount
: 컴포넌트가 브라우저상에서 사라지기 전 호출render()
this.props
와 this.state
에 접근할 수 있으며, react 요소를 반환setState
를 사용하면 안된다.componentDidMount
constructor
getDerivedStateFromProps
componentDidMount
shouldComponentUpdate
this.props
, this.state
로 접근 가능nextProps
, nextProps
로 새로 생성될 값들에 접근 가능getSnapshotBeforeUpdate
componentDidUpdate
preProps
, preState
로 이전 데이터에 접근 가능getSnapshotBeforeUpdate
의 snapshot 값 전달 받을 수 있다.componentWillUnmount
componentDidMount
에서 등록한 이벤트, 생성한 DOM을 제거해야한다componentDidCatch