Skip to content

Commit b66d544

Browse files
authored
Update typechecking-with-proptypes.md
Revert the code style and change the paragraph. And thanks for @bvaughn's advise.
1 parent 400a2c6 commit b66d544

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

content/docs/typechecking-with-proptypes.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ MyComponent.propTypes = {
148148
You can define default values for your `props` by assigning to the special `defaultProps` property:
149149

150150
```javascript
151-
const defaultProps = {
152-
name: 'Stranger'
153-
};
154-
155151
class Greeting extends React.Component {
156152
render() {
157153
return (
@@ -161,7 +157,9 @@ class Greeting extends React.Component {
161157
}
162158

163159
// Specifies the default values for props:
164-
Greeting.defaultProps = defaultProps;
160+
Greeting.defaultProps = {
161+
name: 'Stranger'
162+
};
165163

166164
// Renders "Hello, Stranger":
167165
ReactDOM.render(
@@ -170,10 +168,10 @@ ReactDOM.render(
170168
);
171169
```
172170

173-
You can also put the defaultProps as static props inside React class.
171+
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).
174172

175173
```javascript
176-
class Greeting extends Component {
174+
class Greeting extends React.Component {
177175

178176
static defaultProps = {
179177
name: 'stranger'
@@ -187,6 +185,4 @@ class Greeting extends Component {
187185
}
188186
```
189187

190-
It is considerably more convenient. But non-functional properties are not currently supported for ES2015 classes, you should install extra plugin to support this syntax. If you want to only use strictly ES2015, the former syntax is the better choice.
191-
192188
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`.

0 commit comments

Comments
 (0)