reactで切り替える(ON<->OFF)

4763 ワード

class Toggle extends React.Component {
  constructor() {
    super();
    this.state = {isToggleOn: true};

    // This binding is necessary to make `this` work in the callback
    this.handleClick = this.handleClick.bind(this);
    
    
  }

  handleClick=()=>{ 
    // 이 문법은 `this`가 handleClick 내에서 바인딩되도록 합니다.
    console.log('this is', this)
    this.setState({
      isToggleOn: !this.state.isToggleOn
    });
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        {this.state.isToggleOn ? 'ON' : 'OFF'}
      </button>
    );
  }
}

ReactDOM.render(
  <Toggle />,
  document.getElementById('root')
);