Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1300

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T06:51:09+00:00 2024-11-25T06:51:09+00:00

Use Arrow function, () => {}, to correctly bind this

  • 62k

As part of my role working for AG Grid I get to see a number of repeated issues. One such issue that I have seen multiple times is this.

this.props is undefined in a callback but I know that it does have a value in my component!

What is this?

Let's look at an example of when this can easily occur. Below is a section of the code for a Bar Chart which has added a click event listener.

This example is written in React but you will experience the same in Angular, Typescript and any Class based code.

  class ChartExample extends Component {   constructor(props) {     super(props);      this.state = {       options: {         series: [           {             listeners: {               nodeClick: function (event) {                 // Do something with our props                 console.log("Props are:", this.props)               },             },           },         ],               },     };   }    render() {     return (       <div className="wrapper">         <AgChartsReact options={this.state.options} />       </div>     );   } }  render(<ChartExample test={'Test Value'} />, document.querySelector('#root'));   
Enter fullscreen mode Exit fullscreen mode

The key part of this code is the following section where we define our listeners.

   listeners:    {     nodeClick: function (event) {         // Do something with our props         console.log("Props are:", this.props)     }   }   
Enter fullscreen mode Exit fullscreen mode

If our component receives props then we would expect that these would be logged to the console. However the above code logs undefined when a node is clicked. You can confirm this for yourself in this Plunker.

this.props is undefined within function callback

The arrow function fix

If we make what seems like a purely syntax change, swapping out the function definition for the equivalent arrow format, then try again, suddenly our props are now correctly logged to the console! Updated Plunker

   listeners: {               nodeClick: (event) => {                 // props are defined                 console.log("Props are:", this.props)               },             },   
Enter fullscreen mode Exit fullscreen mode

this.props correctly defined within an arrow function callback

So what is going on here and why does using the syntax (event) => {} suddenly make this.props have the correct value!?

This binding with arrow function

The short answer to why this is works is that by default arrow functions bind this to the value in their surrounding scope. So in our example this will be correctly bound to our component class meaning that this.props will be correctly defined. When we use a standard function definition this is not bound to our component but instead will have the value of the class where our event was fired.

If you want to dive deeper into this topic I would recommend Understanding Arrow Functions in JavaScript

Alternative Fix with bind

It is worth mentioning that you can also fix the original code by explicitly binding this when you define the callback.

  

listeners: {
nodeClick: function (event) {
console.log("Props are:", this)

}.bind(this),
},

Enter fullscreen mode Exit fullscreen mode



Conclusion

If you ever find yourself surprised that this.props, or for that matter, this.anyClassProp is undefined when you are expecting it to contain a certain value then you could check the context in which you are using this. You may find that simply updating a function definition to an arrow function resolves your issue.

As a result of these issues I have made a large update to all our AG Grid examples so that they use arrow functions by default to help avoid confusion for our users.

beginnersjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.