← 문제 목록
중급 React 50P

React State 업데이트 방식은?

React에서 컴포넌트의 상태(state)를 업데이트할 때, setState 메서드를 사용하는 것이 일반적입니다. setState는 비동기적으로 작동하며, 새로운 상태는 이전 상태를 기반으로 계산될 수 있습니다. 다음은 상태를 업데이트하기 위해 setState를 사용하는 간단한 예시입니다:



위 코드에서 상태를 업데이트할 때 어떤 방식이 올바른지 선택하세요.
JAVASCRIPT
class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  increment = () => {
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return <button onClick={this.increment}>Count: {this.state.count}</button>;
  }
}

답변

로그인 후 제출하기
0명 풀이 · 정답률 0%