[react] 생명주기 메서드를 사용하여 컴포넌트의 상태를 초기화하는 방법은 무엇인가요?
import React, { Component } from 'react';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      // 상태 초기화
      count: 0,
      isLoaded: false,
      data: null
    };
  }

  // ...render 및 다른 메서드들
}

위 예시에서 constructor 메서드 내부에서 this.state를 사용하여 초기 상태를 설정합니다. super(props)를 호출하여 부모 클래스의 생성자를 호출해야 합니다. 그 후에 this.state에 초기 상태 객체를 할당합니다.

constructor 메서드는 class 컴포넌트 내에서만 사용할 수 있으며, 함수형 컴포넌트에서는 useState 훅을 사용하여 상태를 초기화합니다.

참고문헌: React 공식 문서 - 생명주기 메서드