react 中的绑定事件

箭头函数

  handleOpen = (e)=> {
    this.setState({
      open: true
    })
  }
  <Button color='primary' onClick={this.handleOpen}>打开模态框</Button>

bind

  handleOpen(e) {
    this.setState({
      open: true
    })
  }

<Button color='primary' onClick={this.handleOpen.bind(this)}>打开模态框</Button>

在构造函数里面 bing

    constructor(props){
      super(props);
      this.handleOpen = this.handleOpen.bind(this);
    }

  handleOpen(e) {
    this.setState({
      open: true
    })
  }
  
<Button color='primary' onClick={this.handleOpen}>打开模态框</Button>

click添加箭头函数

  handleOpen(e) {
    this.setState({
      open: true
    })
  }

<Button color='primary' onClick={()=>{this.handleOpen}}>打开模态框</Button>

猜你喜欢

转载自www.cnblogs.com/ajanuw/p/9416819.html