What is the main purpose of constructor?

The constructor is mainly used for two purposes,

  1. To initialize local state by assigning object to this.state
  2. For binding event handler methods to the instance For example, the below code covers both the above cases,
constructor(props) {
super(props);
// Don't call this.setState() here!
this.state = { counter: 0 };
this.handleClick = this.handleClick.bind(this);
}

Author of content
Edit this page

Made by Michael Sakhniuk

Twitter