When to use/not use .bind() for a class method in React?


In this exercise (“Call this.setState from Another Function”), the code provided includes the following: this.makeSomeFog = this.makeSomeFog.bind(this);The lesson texts states:

This line is necessary because makeSomeFog()'s body contains the word this.

HOWEVER, in other React components, there are class methods that contain this, and yet, their class constructor() DOES NOT contain the special binding call for those functions. One example is the “Random Color Picker” project, where the class contains several methods that use this in their body (ex.: componentDidMount(), isLight() and applyColor()). None of these methods are bound in the constructor().Can someone please explain why, despite containing this, these other class methods do not need to be bound in the class constructor()? The explanation/rule provided in the lesson is not actually followed in these cases, which is making this subject very confusing…Thanks!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.