0 votes
in ReactJS by
What is ithe main purpose of constructor in React JS?

1 Answer

0 votes
by

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);
}
...