InterviewHubPRO
Section Overview
Q417

Question 417 of 445

Part 11: Legacy Architecture, Migration Patterns & Historical Internals (354 - 445)

94%
React & Modern Frontend (445 Qs)•Part 11: Legacy Architecture, Migration Patterns & Historical Internals (354 - 445)

Is it mandatory to define constructor for React component?

No, it is not mandatory. i.e, If you don’t initialize state and you don’t bind methods, you don’t need to implement a constructor for your React component.

Code Example

tsx
import React, { Component } from 'react';

// Constructor is completely OPTIONAL when using class property initializers
export class CleanComponent extends Component<{ greeting: string }> {
  // Class field syntax avoids constructor boilerplate
  state = { count: 0 };

  increment = () => {
    this.setState((prev: any) => ({ count: prev.count + 1 }));
  };

  render() {
    return (
      <button onClick={this.increment}>
        {this.props.greeting}: {this.state.count}
      </button>
    );
  }
}