You get a bonus - 1 coin for daily activity. Now you have 1 coin

What is the difference between the constructor and getInitialState? in REACT

Practice



You should initialise state in the constructor when using ES6 classes, and in the

getInitialState()

method when using

React.createClass()

.

Using ES6 classes:

class MyComponent extends React.Component {
  constructor(props) {
    super(props)
    this.state = { /* initial state */ }
  }
}
Using React.createClass()

:

const MyComponent = React.createClass({
  getInitialState() {
    return { /* initial state */ }
  }
})
Note: React.createClass()

is deprecated and was removed in React v16. Use plain JavaScript classes instead.

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Scripting client side JavaScript, jqvery, BackBone"

Terms: Scripting client side JavaScript, jqvery, BackBone