Tag Archives: bind

Why we must bind prototype functions for event handlers in React?

ref – http://chineseruleof8.com/code/index.php/2020/02/24/this-context/
https://www.freecodecamp.org/news/this-is-why-we-need-to-bind-event-handlers-in-class-components-in-react-f7ea1a6f93eb/

First, some basics.

In unrestricted, es5 environment:

when we call a standalone function, the ‘this’ gets dynamically bound to the global object.
when we call a method of a method, the ‘this’ gets dynamically bound to the global object.

If you had an object (or instance) calling the method, you’ll get the ‘this’ gets bound to the object.

However, if you were to use strict mode, ‘this’ inside functions that are standalone, or within a method, would reference undefined

This happens in React because React uses Modules. Modules are strict by default. Thus, ‘this’ in React is null.

Notice how when we have a reference to the prototype function, the calling environment is global. The calling object is actually Global/Window.
Thus, in strict mode, it becomes undefined, rather than displaying the calling instance.

If we don’t use use strict, result would be:

We see that the calling object is Global (or Window in Elements)
This also holds true in function of function.

Simulating event handler executions

Let’s take a look at an example that simulates an event being passed and our handler being executed:

We first execute prototype function display. Hence, the ‘this’ gets dynamically bound to the calling instance. It exists, and then looks for property ‘name’, which it finds. That is why we can successfully log Saurabh.

Now let’s look at the React situation. Initially you get something like this:

Notice that when you click, the ‘this’ is undefined. WHY!?

The reason is that when handlClick is executed by the web environment, it is being executed in a different environment, and a reference somewhere points to our instance’s display function.

Let’s simulate it like this:

Notice foo is not executing handleClick. Rather another reference is executing it. And that reference’s environment here is global.
But wait a minute! Shouldn’t the ‘this’ value point to the global object, since we are running this in non-strict mode according to the rules of default binding, ‘this’ in standalone functions should be pointing to the global object?

This is the not the case because because when we have another reference pointing to
– constructors,
– static functions,
-prototype methods,
they are executed in strict mode. Getter and setter functions are also executed in strict mode.

And when this happens in the global environment, they are returned undefined. This is why handlers in React components will dynamically point this to undefined. The function is running in strict mode, and the executing reference is global.

this is why in React class components, we do this:

that way, when the web environment creates references to react component method and executes, its ‘this’ will point to the class itself.

2020 way of doing this

ref – https://medium.com/front-end-weekly/do-i-still-need-to-bind-react-functions-in-2019-6d0fe72f40d7

Use arrow functions. setText function gets returned whenever onClick is executed.

Use class field syntax for method declaration:

Notice that onClick references a curried function. This means whenever the web environment receives a user click, it will execute

whereas setText’s this is already bound to MyComponent via arrow syntax