
 출처: codeacademyconstructor(), render(), componentDidMount() 메서드가 실행된다.render(), componentDidUpdate() 메서드가 실행된다.componentWillUnmount() 메서드가 실행된다.컴포넌트의 생애주기 각각의 부분을 의미한다. 각각의 메서드는 React.component class에서 제공하는 메서드로 class 메서드를 생성할 때 사용할 수 있고, lifecycle에 따라 각자의 메서드가 호출된다.
import React from 'react';
export class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = { date: new Date() };
  }
  render() {
    return (
      <div>
        {this.props.isPrecise
          ? this.state.date.toISOString()
          : this.state.date.toLocaleTimeString()}
      </div>
    );
  }
  startInterval() {
    let delay;
    if (this.props.isPrecise) {
      delay = 100;
    } else {
      delay = 1000;
    }
    this.intervalID = setInterval(() => {
      this.setState({ date: new Date() });
    }, delay);
  }
  componentDidMount() {
    this.startInterval();
  }
  componentDidUpdate(prevProps) {
    if (this.props.isPrecise === prevProps.isPrecise) {
      return;
    }
    clearInterval(this.intervalID);
    this.startInterval();
  }
  componentWillUnmount() {
    clearInterval(this.intervalID);
  }
}
ReactDOM.render(<Clock />, document.getElementById('app'));
constructor() mounting 단계에서 일어나는 첫번째 메소드
render() mounting 단계 후반에 일어나는 메서드. 컴포넌트를 최초로 렌더링할때 필요하며 updating 단계에서 컴포넌트를 재실행(re-render)한다.
componentDidMount()
setInterval() 함수는 본인 컴포넌트 내에서 값이 업데이트 되는 경우이므로 componentDidMount에 쓴다.props나 state를 변경하거나 컴포넌트에 props를 전달하는 경우 update를 유발한다.
컴포넌트를 업데이트할 때 쓰는 메서드는 다양한데(updating 메서드 종류), 주로 componentDidUpdate()와 render()를 쓴다.
componentDidUpdate(prevProps, prevState, snapshot) 
componentDidUpdate(prevProps) {
  // Typical usage (don't forget to compare props):
  if (this.props.userID !== prevProps.userID) {
    this.fetchData(this.props.userID);
  }
}
render() 컴포넌트가 업데이트 될 때 render()가 다시 호출되면서 변경된 부분을 보여준다.
compoentWillUnmount()
setInterVal()과 같이 별다른 조치가 없으면 계속 작동하는 함수는 clearInterval()로 제대로 끝내준다.