Skip to content

Rule suggestion: no-state-in-constructor #1810

@hornta

Description

@hornta

INCORRECT

class MyComponent extends Component {
  constructor(props) {
    super(props);

    this.state = {
      counter: props.initialValue
    };
  }
}

CORRECT

class MyComponent extends Component {
  state = {
    counter: this.props.initialValue
  };
}

Why?
It may reduce boilerplate by not having to define a constructor just for initializing the state.

What do you think about this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions