Browse Source

Merge pull request #239 from kinyaying/master

Update typechecking-with-proptypes.md
main
Brian Vaughn 7 years ago
committed by GitHub
parent
commit
130a20363d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      content/docs/typechecking-with-proptypes.md

16
content/docs/typechecking-with-proptypes.md

@ -168,4 +168,20 @@ ReactDOM.render(
);
```
If you are using a Babel transform like [transform-class-properties](https://babeljs.io/docs/plugins/transform-class-properties/) , you can also declare `defaultProps` as static property within a React component class. This syntax has not yet been finalized though and will require a compilation step to work within a browser. For more information, see the [class fields proposal](https://github.com/tc39/proposal-class-fields).
```javascript
class Greeting extends React.Component {
static defaultProps = {
name: 'stranger'
}
render() {
return (
<div>Hello, {this.props.name}</div>
)
}
}
```
The `defaultProps` will be used to ensure that `this.props.name` will have a value if it was not specified by the parent component. The `propTypes` typechecking happens after `defaultProps` are resolved, so typechecking will also apply to the `defaultProps`.

Loading…
Cancel
Save