Truyền Method References giữa các Component
1. Làm cách nào để truyền một trình xử lý sự kiện (như onClick) cho một thành phần?
<button onClick={this.handleClick}>2. Làm thế nào để liên kết một chức năng với một thể hiện thành phần?
class Foo extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
console.log('Click happened');
}
render() {
return <button onClick={this.handleClick}>Click Me</button>;
}
}class Foo extends Component {
// Note: this syntax is experimental and not standardized yet.
handleClick = () => {
console.log('Click happened');
}
render() {
return <button onClick={this.handleClick}>Click Me</button>;
}
}Last updated