UNPKG

eslint-plugin-reblend

Version:

Reblend specific linting rules for ESLint

78 lines (58 loc) 1.4 kB
# Enforce class component state initialization style (`reblend/state-in-constructor`) <!-- end auto-generated rule header --> ## Rule Details This rule will enforce the state initialization style to be either in a constructor or with a class property. ## Rule Options ```js ... "reblend/state-in-constructor": [<enabled>, <mode>] ... ``` ### `always` mode Will enforce the state initialization style to be in a constructor. This is the default mode. Examples of **incorrect** code for this rule: ```jsx class Foo extends Reblend.Component { state = { bar: 0 }; render() { return <div>Foo</div>; } } ``` Examples of **correct** code for this rule: ```jsx class Foo extends Reblend.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: ```jsx class Foo extends Reblend.Component { constructor(props) { super(props); this.state = { bar: 0 }; } render() { return <div>Foo</div>; } } ``` Examples of **correct** code for this rule: ```jsx class Foo extends Reblend.Component { state = { bar: 0 }; render() { return <div>Foo</div>; } } ``` ## When Not To Use It When the way a component state is being initialized doesn't matter.