|
| 1 | +# Enforce defaultProps declarations alphabetical sorting (react/sort-default-props) |
| 2 | + |
| 3 | +Some developers prefer to sort `defaultProps` declarations alphabetically to be able to find necessary declarations easier at a later time. Others feel that it adds complexity and becomes a burden to maintain. |
| 4 | + |
| 5 | +## Rule Details |
| 6 | + |
| 7 | +This rule checks all components and verifies that all `defaultProps` declarations are sorted alphabetically. A spread attribute resets the verification. The default configuration of the rule is case-sensitive. |
| 8 | + |
| 9 | +The following patterns are considered warnings: |
| 10 | + |
| 11 | +```jsx |
| 12 | +var Component = createReactClass({ |
| 13 | +... |
| 14 | + getDefaultProps: function() { |
| 15 | + return { |
| 16 | + z: "z", |
| 17 | + a: "a", |
| 18 | + b: "b" |
| 19 | + }; |
| 20 | + }, |
| 21 | +... |
| 22 | +}); |
| 23 | + |
| 24 | +class Component extends React.Component { |
| 25 | + ... |
| 26 | +} |
| 27 | +Component.defaultProps = { |
| 28 | + z: "z", |
| 29 | + a: "a", |
| 30 | + b: "b" |
| 31 | +}; |
| 32 | + |
| 33 | +class Component extends React.Component { |
| 34 | + static defaultProps = { |
| 35 | + z: "z", |
| 36 | + y: "y", |
| 37 | + a: "a" |
| 38 | + } |
| 39 | + render() { |
| 40 | + return <div />; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +const Component = (props) => (...); |
| 45 | +Component.defaultProps = { |
| 46 | + z: "z", |
| 47 | + y: "y", |
| 48 | + a: "a" |
| 49 | +}; |
| 50 | + |
| 51 | +const defaults = { |
| 52 | + b: "b" |
| 53 | +}; |
| 54 | +const types = { |
| 55 | + a: PropTypes.string, |
| 56 | + b: PropTypes.string, |
| 57 | + c: PropTypes.string' |
| 58 | +}; |
| 59 | +function StatelessComponentWithSpreadInPropTypes({ a, b, c }) { |
| 60 | + return <div>{a}{b}{c}</div>; |
| 61 | +} |
| 62 | +StatelessComponentWithSpreadInPropTypes.propTypes = types; |
| 63 | +StatelessComponentWithSpreadInPropTypes.defaultProps = { |
| 64 | + c: "c", |
| 65 | + a: "a", |
| 66 | + ...defaults, |
| 67 | +}; |
| 68 | +
|
| 69 | +export default class ClassWithSpreadInPropTypes extends BaseClass { |
| 70 | + static propTypes = { |
| 71 | + a: PropTypes.string, |
| 72 | + b: PropTypes.string, |
| 73 | + c: PropTypes.string, |
| 74 | + d: PropTypes.string, |
| 75 | + e: PropTypes.string, |
| 76 | + f: PropTypes.string |
| 77 | + } |
| 78 | + static defaultProps = { |
| 79 | + b: "b", |
| 80 | + a: "a", |
| 81 | + ...c.defaultProps, |
| 82 | + f: "f", |
| 83 | + e: "e", |
| 84 | + ...d.defaultProps |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | +
|
| 89 | +The following patterns are considered okay and do **not** cause warnings: |
| 90 | +
|
| 91 | +```jsx |
| 92 | +var Component = createReactClass({ |
| 93 | +... |
| 94 | + getDefaultProps: function() { |
| 95 | + return { |
| 96 | + a: "a", |
| 97 | + b: "b", |
| 98 | + c: "c" |
| 99 | + }; |
| 100 | + }, |
| 101 | +... |
| 102 | +}); |
| 103 | +
|
| 104 | +class Component extends React.Component { |
| 105 | + ... |
| 106 | +} |
| 107 | +Component.defaultProps = { |
| 108 | + a: "a", |
| 109 | + b: "b", |
| 110 | + c: "c" |
| 111 | +}; |
| 112 | +
|
| 113 | +class Component extends React.Component { |
| 114 | + static defaultProps = { |
| 115 | + a: PropTypes.any, |
| 116 | + b: PropTypes.any, |
| 117 | + c: PropTypes.any |
| 118 | + } |
| 119 | + render() { |
| 120 | + return <div />; |
| 121 | + } |
| 122 | +} |
| 123 | +
|
| 124 | +const Component = (props) => (...); |
| 125 | +Component.defaultProps = { |
| 126 | + a: "a", |
| 127 | + y: "y", |
| 128 | + z: "z" |
| 129 | +}; |
| 130 | +
|
| 131 | +const defaults = { |
| 132 | + b: "b" |
| 133 | +}; |
| 134 | +const types = { |
| 135 | + a: PropTypes.string, |
| 136 | + b: PropTypes.string, |
| 137 | + c: PropTypes.string' |
| 138 | +}; |
| 139 | +function StatelessComponentWithSpreadInPropTypes({ a, b, c }) { |
| 140 | + return <div>{a}{b}{c}</div>; |
| 141 | +} |
| 142 | +StatelessComponentWithSpreadInPropTypes.propTypes = types; |
| 143 | +StatelessComponentWithSpreadInPropTypes.defaultProps = { |
| 144 | + a: "a", |
| 145 | + c: "c", |
| 146 | + ...defaults, |
| 147 | +}; |
| 148 | + |
| 149 | +export default class ClassWithSpreadInPropTypes extends BaseClass { |
| 150 | + static propTypes = { |
| 151 | + a: PropTypes.string, |
| 152 | + b: PropTypes.string, |
| 153 | + c: PropTypes.string, |
| 154 | + d: PropTypes.string, |
| 155 | + e: PropTypes.string, |
| 156 | + f: PropTypes.string |
| 157 | + } |
| 158 | + static defaultProps = { |
| 159 | + a: "a", |
| 160 | + b: "b", |
| 161 | + ...c.defaultProps, |
| 162 | + e: "e", |
| 163 | + f: "f", |
| 164 | + ...d.defaultProps |
| 165 | + } |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +## Rule Options |
| 170 | + |
| 171 | +```js |
| 172 | +... |
| 173 | +"react/sort-default-props": [<enabled>, { |
| 174 | + "ignoreCase": <boolean>, |
| 175 | +}] |
| 176 | +... |
| 177 | +``` |
| 178 | + |
| 179 | +### `ignoreCase` |
| 180 | + |
| 181 | +When `true` the rule ignores the case-sensitivity of the declarations order. |
| 182 | + |
| 183 | +## When not to use |
| 184 | + |
| 185 | +This rule is a formatting preference and not following it won't negatively affect the quality of your code. If alphabetizing `defaultProps` declarations isn't a part of your coding standards, then you can leave this rule off. |
0 commit comments