|
| 1 | +# Enforce a defaultProps definition for every prop that is not a required prop (require-default-props) |
| 2 | + |
| 3 | +This rule aims to ensure that any non-required `PropType` declaration of a component has a corresponding `defaultProps` value. |
| 4 | + |
| 5 | +One advantage of `defaultProps` over custom default logic in your code is that `defaultProps` are resolved by React before the `PropTypes` typechecking happens, so typechecking will also apply to your `defaultProps`. |
| 6 | +The same also holds true for stateless functional components: default function parameters do not behave the same as `defaultProps` and thus using `defaultProps` is still preferred. |
| 7 | + |
| 8 | +To illustrate, consider the following example: |
| 9 | + |
| 10 | +With `defaultProps`: |
| 11 | +```js |
| 12 | +const HelloWorld = ({ name }) => ( |
| 13 | + <h1>Hello, {name.first} {name.last}!</h1> |
| 14 | +); |
| 15 | + |
| 16 | +HelloWorld.propTypes = { |
| 17 | + name: React.PropTypes.shape({ |
| 18 | + first: React.PropTypes.string, |
| 19 | + last: React.PropTypes.string, |
| 20 | + }) |
| 21 | +}; |
| 22 | + |
| 23 | +HelloWorld.defaultProps = { |
| 24 | + name: 'john' |
| 25 | +}; |
| 26 | + |
| 27 | +// Logs: |
| 28 | +// Invalid prop `name` of type `string` supplied to `HelloWorld`, expected `object`. |
| 29 | +ReactDOM.render(<HelloWorld />, document.getElementById('app')); |
| 30 | +``` |
| 31 | + |
| 32 | +Without `defaultProps`: |
| 33 | +```js |
| 34 | +const HelloWorld = ({ name = 'John Doe' }) => ( |
| 35 | + <h1>Hello, {name.first} {name.last}!</h1> |
| 36 | +); |
| 37 | + |
| 38 | +HelloWorld.propTypes = { |
| 39 | + name: React.PropTypes.shape({ |
| 40 | + first: React.PropTypes.string, |
| 41 | + last: React.PropTypes.string, |
| 42 | + }) |
| 43 | +}; |
| 44 | + |
| 45 | +// Nothing is logged, renders: |
| 46 | +// "Hello,!" |
| 47 | +ReactDOM.render(<HelloWorld />, document.getElementById('app')); |
| 48 | +``` |
| 49 | + |
| 50 | +## Rule Details |
| 51 | + |
| 52 | +The following patterns are considered warnings: |
| 53 | + |
| 54 | +```js |
| 55 | +function MyStatelessComponent({ foo, bar }) { |
| 56 | + return <div>{foo}{bar}</div>; |
| 57 | +} |
| 58 | + |
| 59 | +MyStatelessComponent.propTypes = { |
| 60 | + foo: React.PropTypes.string.isRequired, |
| 61 | + bar: React.PropTypes.string |
| 62 | +}; |
| 63 | +``` |
| 64 | + |
| 65 | +```js |
| 66 | +var Greeting = React.createClass({ |
| 67 | + render: function() { |
| 68 | + return <div>Hello {this.props.foo} {this.props.bar}</div>; |
| 69 | + }, |
| 70 | + |
| 71 | + propTypes: { |
| 72 | + foo: React.PropTypes.string, |
| 73 | + bar: React.PropTypes.string |
| 74 | + }, |
| 75 | + |
| 76 | + getDefaultProps: function() { |
| 77 | + return { |
| 78 | + foo: "foo" |
| 79 | + }; |
| 80 | + } |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +```js |
| 85 | +class Greeting extends React.Component { |
| 86 | + render() { |
| 87 | + return ( |
| 88 | + <h1>Hello, {this.props.foo} {this.props.bar}</h1> |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +Greeting.propTypes = { |
| 94 | + foo: React.PropTypes.string, |
| 95 | + bar: React.PropTypes.string |
| 96 | +}; |
| 97 | + |
| 98 | +Greeting.defaultProps = { |
| 99 | + foo: "foo" |
| 100 | +}; |
| 101 | +``` |
| 102 | + |
| 103 | +```js |
| 104 | +class Greeting extends React.Component { |
| 105 | + render() { |
| 106 | + return ( |
| 107 | + <h1>Hello, {this.props.foo} {this.props.bar}</h1> |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + static propTypes = { |
| 112 | + foo: React.PropTypes.string, |
| 113 | + bar: React.PropTypes.string.isRequired |
| 114 | + }; |
| 115 | + |
| 116 | + static defaultProps = { |
| 117 | + foo: "foo" |
| 118 | + }; |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +```js |
| 123 | +type Props = { |
| 124 | + foo: string, |
| 125 | + bar?: string |
| 126 | +}; |
| 127 | + |
| 128 | +function MyStatelessComponent(props: Props) { |
| 129 | + return <div>Hello {props.foo} {props.bar}</div>; |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +The following patterns are not considered warnings: |
| 134 | + |
| 135 | +```js |
| 136 | +function MyStatelessComponent({ foo, bar }) { |
| 137 | + return <div>{foo}{bar}</div>; |
| 138 | +} |
| 139 | + |
| 140 | +MyStatelessComponent.propTypes = { |
| 141 | + foo: React.PropTypes.string.isRequired, |
| 142 | + bar: React.PropTypes.string.isRequired |
| 143 | +}; |
| 144 | +``` |
| 145 | + |
| 146 | +```js |
| 147 | +function MyStatelessComponent({ foo, bar }) { |
| 148 | + return <div>{foo}{bar}</div>; |
| 149 | +} |
| 150 | + |
| 151 | +MyStatelessComponent.propTypes = { |
| 152 | + foo: React.PropTypes.string.isRequired, |
| 153 | + bar: React.PropTypes.string |
| 154 | +}; |
| 155 | + |
| 156 | +MyStatelessComponent.defaultProps = { |
| 157 | + bar: 'some default' |
| 158 | +}; |
| 159 | +``` |
| 160 | + |
| 161 | +```js |
| 162 | +type Props = { |
| 163 | + foo: string, |
| 164 | + bar?: string |
| 165 | +}; |
| 166 | + |
| 167 | +function MyStatelessComponent(props: Props) { |
| 168 | + return <div>Hello {props.foo} {props.bar}</div>; |
| 169 | +} |
| 170 | + |
| 171 | +MyStatelessComponent.defaultProps = { |
| 172 | + bar: 'some default' |
| 173 | +}; |
| 174 | +``` |
| 175 | + |
| 176 | +```js |
| 177 | +function NotAComponent({ foo, bar }) {} |
| 178 | + |
| 179 | +NotAComponent.propTypes = { |
| 180 | + foo: React.PropTypes.string, |
| 181 | + bar: React.PropTypes.string.isRequired |
| 182 | +}; |
| 183 | +``` |
| 184 | + |
| 185 | +## When Not To Use It |
| 186 | + |
| 187 | +If you don't care about using `defaultsProps` for your component's props that are not required, you can disable this rule. |
| 188 | + |
| 189 | +# Resources |
| 190 | +- [Official React documentation on defaultProps](https://facebook.github.io/react/docs/typechecking-with-proptypes.html#default-prop-values) |
0 commit comments