---
url: /docs/guide/usage/linter/rules/react/state-in-constructor.md
---

### What it does

Enforces the state initialization style to be either in a
constructor or with a class property.

This rule is not relevant for function components, and so can potentially be
disabled for modern React codebases.

### Why is this bad?

Inconsistent state initialization styles can make the codebase harder to maintain and understand.
This rule enforces a consistent pattern across React class components.

### Examples

Examples of **incorrect** code for this rule by default, with `"always"` mode:

```jsx
class Foo extends React.Component {
  state = { bar: 0 };
  render() {
    return <div>Foo</div>;
  }
}
```

Examples of **correct** code for this rule by default, with `"always"` mode:

```jsx
class Foo extends React.Component {
  constructor(props) {
    super(props);
    this.state = { bar: 0 };
  }
  render() {
    return <div>Foo</div>;
  }
}
```

#### `"never"` mode

Will enforce the state initialization style to be with a class property.

Examples of **incorrect** code for this rule with `"never"` mode:

```jsx
class Foo extends React.Component {
  constructor(props) {
    super(props);
    this.state = { bar: 0 };
  }
  render() {
    return <div>Foo</div>;
  }
}
```

Examples of **correct** code for this rule with `"never"` mode:

```jsx
class Foo extends React.Component {
  state = { bar: 0 };
  render() {
    return <div>Foo</div>;
  }
}
```

## Configuration

This rule accepts one of the following string values:

type: `"always" | "never"`

## How to use

## Version

This rule was added in v1.26.0.

## References
