Results 1 to 3 of 3

Thread: State the difference between getIntialState() and constructor()?

  1. #1

    Default State the difference between getIntialState() and constructor()?

    State the difference between getIntialState() and constructor()?

  2. #2

    Default

    Hi Friends,
    The two approaches are not interchangeable. You should initialize state in the constructor when using ES6 classes, and define the getInitialState method when using React.createClass.

  3. #3
    Join Date
    Apr 2020
    Posts
    704

    Default

    The difference between constructor and getInitialState is the difference between ES6 and ES5 itself.
    getInitialState is used with React.createClass and
    constructor is used with React.Component.

    Hence the question boils down to advantages/disadvantages of using ES6 or ES5.

    Let's look at the difference in code

    ES5

    var TodoApp = React.createClass({
    propTypes: {
    title: PropTypes.string.isRequired
    },
    getInitialState () {
    return {
    items: []
    };
    }
    });

    ES6

    class TodoApp extends React.Component {
    constructor () {
    super()
    this.state = {
    items: []
    }
    }
    };

Similar Threads

  1. how to design a real state website?
    By benk in forum Web Design Solutions
    Replies: 14
    Last Post: 11-15-2017, 11:06 AM
  2. [WTS] Powerful Dell Dual Core servers(HostingSource)| state-of-the-art hardware!
    By Hostingsource in forum Web Hosting and Related Offers Forum
    Replies: 0
    Last Post: 11-27-2015, 04:50 AM
  3. #1 State-Of-The-Art Servers - HostDepot - Maximum Performance & Uptime + BONUSES!
    By HostDepot.Danie in forum Web Hosting and Related Offers Forum
    Replies: 0
    Last Post: 03-13-2014, 08:04 AM
  4. #1 State-Of-The-Art Servers - HostDepot - Maximum Performance & Uptime + BONUSES!
    By HostDepot.Danie in forum Web Hosting and Related Offers Forum
    Replies: 0
    Last Post: 10-31-2013, 11:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •