in React JS by
Q:

What is the difference between constructor and getInitialState?

1 Answer

0 votes
by

You should initialize state in the constructor when using ES6 classes, and 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 */ }
  }
})
Click here to read more about React JS
Click here to read more about Insurance

Related questions

0 votes
asked Mar 30, 2020 in React JS by amita rallin
0 votes
asked Mar 3, 2020 in React JS by miceperry
0 votes
asked Mar 1, 2020 in React JS by RShastri
0 votes
asked Jul 2, 2019 in React JS by Venkatshastri
0 votes
asked Nov 26, 2019 in React JS by AdilsonLima
...