DEV Community

Bulent Gorkem
Bulent Gorkem

Posted on

3

Lambda function use in React Class Components

React functional components have become more popular than React class components in recent times. Hence we don’t need to rely on use of often misunderstood "this" keyword to access member variables, methods. We used to add a lot of mysterious bind(this) methods to our handlers in constructors before.

   this.checkedHandler = this.checkedHandler.bind(this);

Enter fullscreen mode Exit fullscreen mode

Together with lambda syntax used in class methods we don't need this ceremony anymore.

If you want to understand why, the code sample below explains briefly.

import React from "react";

export default class App extends React.Component {
  constructor(props: React.PropsWithChildren) {
    super(props);
  }

  handleThis() {
    console.log("handle ", this);
  }

  handleThisWithLambda = ()=> {
    console.log('handle that', this)
  }

  render() {
    return (
      <div className="App">

        <button onClick={this.handleThis}>Handle This </button>
        <button onClick={this.handleThisWithLambda}>Handle That </button> </div>
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

There are 2 methods: handleThis and handleThisWithLambda

First one prints undefined as this, because in strict-mode this value is undefined when run outside of any object, ie. the event handler, unless it was bound by the function called, hence the use of ...bind.this( )

The second one with the lambda syntax though prints the correct App instance we are running in.

What is the difference?

If you copy-paste this code to a code transpiler, like typescript playground which converts it old ECMAScript 2017 code, you will spot the difference in how lambda syntax is translated inside the class code

export default class App extends React.Component {
    constructor(props) {
        super(props);
        this.handleThisWithLambda = () => {
            console.log('handle that', this);
        };
    }
    handleThis() {
        console.log("handle ", this);
    }
    ...
}

Enter fullscreen mode Exit fullscreen mode

As you can see handleThisWithLambda code moved into the constructor function, hence created with a closure to the this instance. So it is already doing the same job bind.this was doing previously for the legacy method case.

In React Functional components, you don't need this or bind.this anymore, as every thing is inside the same function closure anyway.

Hope this helps!

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay