Practice
There are 3 possible ways to achieve this:
class Component extends React.Componenet {
constructor(props) {
super(props)
this.handleClick = this.handleClick.bind(this)
}
handleClick() {
// ...
}
}
handleClick = () => {
console.log('this is:', this)
}
{'Click me'}
this.handleClick(event)}>
{'Click me'}
Note. If the callback is passed as a prop to child components, those components may perform an extra re-render. In such cases it is preferable to go with the .bind()
or public class fields syntax approach for performance reasons.
Comments