DEV Community

dmanias
dmanias

Posted on

React bind(this)

In React, whenever you define an event handler that uses "this", you need to add "this.methodName = this.methodName.bind(this)" to your constructor function.

This is not React-specific behavior; it is a part of how functions work in JavaScript. Generally, if you refer to a method without () after it, such as onClick={this.handleClick}, you should bind that method.

Top comments (1)

Collapse
 
rachid3alaoui profile image
Rachid Amrani Alaoui

Thank you.