class Counter extends React.Component {
  //Class의 생성자
  constructor(props) {
    super(props);
    //state는 초기화가 필요하다.
    this.state = {
      value : 0
    }
    this.handleClick = this.handleClick.bind(this);
    //binding은 필수!
  }
  handleClick(){
    this.setState({
      value : this.state.value + 1
    });
    //this.setState를 이용하여 값의 변화를 주어야한다. 반드시!!
  }
  render(){
    return(
      <div>
        <h2>{this.state.value}</h2>
        <button onClick={this.handleClick}>Press Me</button>
      </div>
    );
  }
};
class App extends React.Component {
  render() {
    return (
      <Counter />
    );
  }
};

ReactDOM.render(
  <App></App>,
  document.getElementById("root")
);

results matching ""

    No results matching ""