React & Modern Frontend (445 Qs)•Part 11: Legacy Architecture, Migration Patterns & Historical Internals (354 - 445)
What is the main purpose of constructor?
The constructor is mainly used for two purposes,
vbnet
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,
javascript
constructor(props) {
super(props);
// Don't call this.setState() here!
this.state = { counter: 0 };
this.handleClick = this.handleClick.bind(this);
}