What is the difference between constructor and getInitialState?
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 */};},});
Note: React.createClass()
is deprecated and removed in React v16. Use plain JavaScript classes instead.
January 03, 2022
319
Read more
What is React?
November 06, 2022
ReactJSHow to programmatically trigger click event in React?
November 06, 2022
ReactJS