|
| 1 | +# Enforces where React component static properties should be positioned. (static-property-placement) |
| 2 | + |
| 3 | +This rule allows you to enforce where `childContextTypes`, `contextTypes`, `contextType`, `defaultProps`, `displayName`, |
| 4 | +and `propTypes` are declared in an ES6 class. |
| 5 | + |
| 6 | + |
| 7 | +## Rule Details |
| 8 | + |
| 9 | +By default, this rule will check for and warn about declaring any of the above properties outside of the class body. |
| 10 | + |
| 11 | +There are three key options are `static public field`, `static getter`, and `property assignment`. |
| 12 | + |
| 13 | +### When `static public field` is enabled (default): |
| 14 | + |
| 15 | +Examples of **incorrect** code for this rule: |
| 16 | + |
| 17 | +```js |
| 18 | +class MyComponent extends React.Component { |
| 19 | + static get childContextTypes() { /*...*/ } |
| 20 | + static get contextTypes() { /*...*/ } |
| 21 | + static get contextType() { /*...*/ } |
| 22 | + static get displayName() { /*...*/ } |
| 23 | + static get defaultProps() { /*...*/ } |
| 24 | + static get propTypes() { /*...*/ } |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +```js |
| 29 | +class MyComponent extends React.Component { /*...*/ } |
| 30 | +MyComponent.childContextTypes = { /*...*/ }; |
| 31 | +MyComponent.contextTypes = { /*...*/ }; |
| 32 | +MyComponent.contextType = { /*...*/ }; |
| 33 | +MyComponent.displayName = "Hello"; |
| 34 | +MyComponent.defaultProps = { /*...*/ }; |
| 35 | +MyComponent.propTypes = { /*...*/ }; |
| 36 | +``` |
| 37 | + |
| 38 | +Examples of **correct** code for this rule: |
| 39 | + |
| 40 | +```js |
| 41 | +class MyComponent extends React.Component { |
| 42 | + static childContextTypes = { /*...*/ }; |
| 43 | + static contextTypes = { /*...*/ }; |
| 44 | + static contextType = { /*...*/ }; |
| 45 | + static displayName = "Hello"; |
| 46 | + static defaultProps = { /*...*/ }; |
| 47 | + static propTypes = { /*...*/ }; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### When `static getter` is enabled: |
| 52 | + |
| 53 | +Examples of **incorrect** code for this rule: |
| 54 | + |
| 55 | +```js |
| 56 | +class MyComponent extends React.Component { |
| 57 | + static childContextTypes = { /*...*/ }; |
| 58 | + static contextTypes = { /*...*/ }; |
| 59 | + static contextType = { /*...*/ }; |
| 60 | + static displayName = "Hello"; |
| 61 | + static defaultProps = { /*...*/ }; |
| 62 | + static propTypes = { /*...*/ }; |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +```js |
| 67 | +class MyComponent extends React.Component { /*...*/ } |
| 68 | +MyComponent.childContextTypes = { /*...*/ }; |
| 69 | +MyComponent.contextTypes = { /*...*/ }; |
| 70 | +MyComponent.contextType = { /*...*/ }; |
| 71 | +MyComponent.displayName = "Hello"; |
| 72 | +MyComponent.defaultProps = { /*...*/ }; |
| 73 | +MyComponent.propTypes = { /*...*/ }; |
| 74 | +``` |
| 75 | + |
| 76 | +Examples of **correct** code for this rule: |
| 77 | + |
| 78 | +```js |
| 79 | +class MyComponent extends React.Component { |
| 80 | + static get childContextTypes() { /*...*/ } |
| 81 | + static get contextTypes() { /*...*/ } |
| 82 | + static get contextType() { /*...*/ } |
| 83 | + static get displayName() { /*...*/ } |
| 84 | + static get defaultProps() { /*...*/ } |
| 85 | + static get propTypes() { /*...*/ } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### When `property assignment` is enabled: |
| 90 | + |
| 91 | +Examples of **incorrect** code for this rule: |
| 92 | + |
| 93 | +```js |
| 94 | +class MyComponent extends React.Component { |
| 95 | + static childContextTypes = { /*...*/ }; |
| 96 | + static contextTypes = { /*...*/ }; |
| 97 | + static contextType = { /*...*/ }; |
| 98 | + static displayName = "Hello"; |
| 99 | + static defaultProps = { /*...*/ }; |
| 100 | + static propTypes = { /*...*/ }; |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +```js |
| 105 | +class MyComponent extends React.Component { |
| 106 | + static get childContextTypes() { /*...*/ } |
| 107 | + static get contextTypes() { /*...*/ } |
| 108 | + static get contextType() { /*...*/ } |
| 109 | + static get displayName() { /*...*/ } |
| 110 | + static get defaultProps() { /*...*/ } |
| 111 | + static get propTypes() { /*...*/ } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +Examples of **correct** code for this rule: |
| 116 | + |
| 117 | +```js |
| 118 | +class MyComponent extends React.Component { /*...*/ } |
| 119 | +MyComponent.childContextTypes = { /*...*/ }; |
| 120 | +MyComponent.contextTypes = { /*...*/ }; |
| 121 | +MyComponent.contextType = { /*...*/ }; |
| 122 | +MyComponent.displayName = "Hello"; |
| 123 | +MyComponent.defaultProps = { /*...*/ }; |
| 124 | +MyComponent.propTypes = { /*...*/ }; |
| 125 | +``` |
| 126 | + |
| 127 | +### Options |
| 128 | + |
| 129 | +``` |
| 130 | +... |
| 131 | +"react/static-property-placement": [<enabled>] // `static public field` enabled |
| 132 | +... |
| 133 | +``` |
| 134 | + |
| 135 | +or alternatively: |
| 136 | + |
| 137 | +``` |
| 138 | +... |
| 139 | +"react/static-property-placement": [<enabled>, <string>] |
| 140 | +... |
| 141 | +``` |
| 142 | + |
| 143 | +or alternatively: |
| 144 | + |
| 145 | +``` |
| 146 | +... |
| 147 | +"react/static-property-placement": [<enabled>, <string>, { |
| 148 | + childContextTypes: <string>, |
| 149 | + contextTypes: <string>, |
| 150 | + contextType: <string>, |
| 151 | + defaultProps: <string>, |
| 152 | + displayName: <string>, |
| 153 | + propTypes: <string>, |
| 154 | +}] |
| 155 | +... |
| 156 | +``` |
| 157 | +The `<string>` value must be one these options: |
| 158 | +* `static public field` |
| 159 | +* `static getter` |
| 160 | +* `property assignment` |
| 161 | + |
| 162 | +The `options` schema defined above allows you to specify different rules for the different property fields available. |
| 163 | + |
| 164 | +##### Example configuration: |
| 165 | +_This is only an example, we do not recommend this as a configuration._ |
| 166 | +``` |
| 167 | +... |
| 168 | +"react/static-property-placement": ["warn", "property assignment", { |
| 169 | + childContextTypes: "static getter", |
| 170 | + contextTypes: "static public field", |
| 171 | + contextType: "static public field", |
| 172 | + displayName: "static public field", |
| 173 | +}] |
| 174 | +... |
| 175 | +``` |
| 176 | + |
| 177 | +Based on the above configuration: |
| 178 | +* `defaultProps` and `propTypes` will both enforce the `property assignment` rule. |
| 179 | +* `childContextTypes` will enforce the `static getter` rule. |
| 180 | +* `contextTypes`, `contextType`, and `displayName` will enforce the `static public field` rule. |
| 181 | + |
| 182 | +## When Not To Use It |
| 183 | + |
| 184 | +If you have no placement preference for React's static class properties. |
| 185 | + |
0 commit comments