react/state-in-constructor
eslint-plugin-react
No autofix
eslint.config.js
import react from "eslint-plugin-react";
export default [
{
plugins: { react },
rules: {
"react/state-in-constructor": "error",
},
},
];class Foo extends React.Component {
state = { bar: 0 }
render() {
return <div>Foo</div>
}
}class Foo extends React.Component {
constructor(props) {
super(props)
this.state = { bar: 0 }
}
render() {
return <div>Foo</div>
}
}class Foo extends React.Component {
constructor(props) {
super(props)
this.state = { bar: 0 }
}
render() {
return <div>Foo</div>
}
}class Foo extends React.Component {
state = { bar: 0 }
render() {
return <div>Foo</div>
}
}Examples from the ESLint documentation.
react/state-in-constructor
eslint-plugin-react
import react from "eslint-plugin-react";
export default [
{
plugins: { react },
rules: {
"react/state-in-constructor": "error",
},
},
];react/state-in-constructor
react plugin · style
{
"plugins": [
"react"
],
"rules": {
"react/state-in-constructor": "error"
}
}Biome does not have this rule.
Disallow usage of setState in componentWillUpdate
Enforce all defaultProps have a corresponding non-required PropType
Enforce consistent usage of destructuring assignment of props, state, and context
Enforce boolean attributes notation in JSX