Lesson

Bind React class component methods to proper `this` without calling bind() in constructor

One of the common pitfalls when learning React is related to event handlers losing the reference to this pointing to the component when called. Common approach to solving this issue is to use a constructor and .bind() function to bind all class methods to...

Duration
2 min
Access
Free
Transcript
Available

One of the common pitfalls when learning React is related to event handlers losing the reference to this pointing to the component when called. Common approach to solving this issue is to use a constructor and .bind() function to bind all class methods to this object, referring to the component itself.

It sometimes leads to a constructor which sole purpose is to bind a bunch of methods.

In this lesson we are going to learn how to use a public class field syntax (currently available in Babel stage-2 and enabled by default in create-react-app) to avoid having to create unnecessary constructors in order to bind a React component method to proper this