Categories
JavaScript Answers

How to fix the Expected ‘this’ to be used by class method error with JavaScript?

Spread the love

Sometimes, we want to fix the Expected ‘this’ to be used by class method error with JavaScript.

In this article, we’ll look at how to fix the Expected ‘this’ to be used by class method error with JavaScript.

How to fix the Expected ‘this’ to be used by class method error with JavaScript?

To fix the Expected ‘this’ to be used by class method error with JavaScript, we should change the function causing the error to an arrow function.

For instance, we write

const getUrlParams = (queryString) => {
  //...
};

to define the getUrlParams as an arrow function.

Then if it doesn’t reference this, then the error would go away.

Conclusion

To fix the Expected ‘this’ to be used by class method error with JavaScript, we should change the function causing the error to an arrow function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *