What is the difference between using constructor vs getInitialState in React / React Native?

Answer

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: []
}
}
};

All REACT NATIVE Questions

Ask your interview questions on react-native

Write Your comment or Questions if you want the answers on react-native from react-native Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---