Where to declare variable in Reactjs?

Posted by Ramesh Singh | Updated on

In this example, I have shared where to declare variable in Reactjs? Using ES6 syntax in React does not bind this to user-defined functions however it will bind this to the component lifecycle methods.

So the function that you declared will not have the same context as the class and trying to access this will not give you what you are expecting. For getting the context of class you have to bind the context of the function or use arrow functions.

Method 1

Method 2

If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers