Pass event handlers and other functions as props to child components:
<button onClick={this.handleClick}>
There are several ways to make sure functions have access to component attributes like this.props and this.state, depending on which syntax and build steps you are usin
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>;
}
}
class Foo extends Component {
handleClick() {
console.log('Click happened');
}
render() {
return <button onClick={this.handleClick.bind(this)}>Click Me</button>;
}
}
If you have an event handler such as onClick or onScroll and want to prevent the callback from being fired too quickly, then you can limit the rate at which callback is executed. This can be done by using: