1

In JavaScript, we are not allowed to use variables and we can only define methods. Also if we want to initialise a variable, we should do it either in the constructor or the prototype of the class.But in ReactJs we can initialise state outside constructor.

Is ReactJs classes like class A extends React.Component{} different from normal javascript classes?

AndrewL64
  • 15,794
  • 8
  • 47
  • 79
  • 1
    It is class properties syntax, supported by babel and is an ES7 syntax. You can refer https://stackoverflow.com/questions/45048879/specifying-variable-outside-of-render-in-react-class/45048994#45048994 – Shubham Khatri Dec 26 '18 at 11:08
  • This article [React.createClass vs. ES6 Class Components](https://www.fullstackreact.com/articles/react-create-class-vs-es6-class-components/) gives a really good explanation on the history as well as the differences of the two classes.Thought of extracting a few paragraphs from this article and posting it as an answer but I think a complete read of the article would be better instead. – AndrewL64 Dec 26 '18 at 11:09
  • 1
    @ShubhamKhatri Except that it didn't make it into ES7, it's still a proposal only :-) – Bergi Dec 26 '18 at 11:16

1 Answers1

-1

You can refer Class variables in React with ES6

JS by default does not support declaring variables inside class. In case of React, it uses Babel transpiler which supports that syntax.

Ashish
  • 4,206
  • 16
  • 45