What is the difference between constructor and getInitialState?

Posted by Prem Tiwari | Updated on

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:

Using React.createClass():

Note: React.createClass() is deprecated and removed in React v16. Use plain JavaScript classes instead.


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