Thanks to visit codestin.com
Credit goes to github.com

Skip to content

react向组件传递函数方式 #29

@Clearives

Description

@Clearives

react向组件传递函数方式

bind

constructor(props) {
super(props);
this.state = {
  value: 0
};
this.handleClick2 = this.handleClick1.bind(this);
}

箭头函数

handleClick3 = () => {
  console.log(this);
};

直接传递

handleClick1() {
  console.log(this);
}
<button onClick={this.handleClick1}>click 1</button>
<button onClick={this.handleClick2}>click 2</button>
<button onClick={this.handleClick3}>click 3</button>

点击三个按钮输出结果如下图:
GkrOxI.png

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions