EventHandling


EventHandling


  • キャラメルボックスとして作成
  • onClick , onMouseEnter , ...

  • イベントに接続されたjsコードは関数です
  • アクティビティ={関数}

  • 実際のDOM要素のみに適用
  • 反応素子用の場合はpropsを直接伝える

  • 関数構成部品に適用(onClick)
  •     function Component() {
                return (
                  <div>
                    <button
                      onClick={() => {
                        console.log("hello")
                      }}
                    >
                      클릭
                    </button>
                  </div>
                )
              }
  • 適合部品
  •     class Component extends React.Component{
        
        state = { count: 0 }
        
        render() {
                  return (
                   <div>
                      <p>{this.state.count}</p>
                      <button
                        onClick={() => {
                          console.log("hello")
                          this.setState((state) => ({
                            ...state,
                            count: state.count + 1,
                          }))
                        }}
                      >
                        클릭
                      </button>
                    </div>
                  )
                }
        }
  • 外部で関数を個別に定義する場合
  •         class Component extends React.Component{
            
            state = { count: 0 }
            
            render() {
                      return (
                       <div>
                          <p>{this.state.count}</p>
                          <button
                            onClick={() => {
                              
                            }}
                          >
                            클릭
                          </button>
                        </div>
                      )
                    }
            
            click() {
                      console.log("hello")
                      this.setState((state) => ({
                        ...state,
                        count: state.count + 1,
                      }))
                    }
            }
    そうすると、thisはundefinedエラーが発生します.
    解決策
  • bind作業を行う
  •         class Component extends React.Component{
            
            state = { count: 0 }
            
            constructor(props){
            	super(props);
            	this.click = this.click.bind(this)
            }
            
            render() {
                      return (
                       <div>
                          <p>{this.state.count}</p>
                          <button
                            onClick={() => {
                              
                            }}
                          >
                            클릭
                          </button>
                        </div>
                      )
                    }
            
            click() {
                      console.log("hello")
                      this.setState((state) => ({
                        ...state,
                        count: state.count + 1,
                      }))
                    }
            }
    使用
  • ArrowFunction.
  •         class Component extends React.Component {
                    state = {
                      count: 0,
                    }
            
            render() {
                      return (
                        <div>
                          <p>{this.state.count}</p>
                          <button onClick={this.click}>클릭</button>
                        </div>
                      )
                    }
            
            click = () => {
                      console.log("hello")
                      this.setState((state) => ({
                        ...state,
                        count: state.count + 1,
                      }))
                    }
            }