Skip to content

Commit 3e5e150

Browse files
committed
sync with upstream to 2017.01.05
1 parent 2542c26 commit 3e5e150

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

react/README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,9 @@
187187

188188
## Quotes 单引号还是双引号
189189

190-
- 对于JSX属性值总是使用双引号(`"`), 其他均使用单引号. eslint: [`jsx-quotes`](http://eslint.org/docs/rules/jsx-quotes)
190+
- 对于JSX属性值总是使用双引号(`"`), 其他均使用单引号(`'`). eslint: [`jsx-quotes`](http://eslint.org/docs/rules/jsx-quotes)
191191

192-
> 为什么? JSX属性 [不能包括转译的引号](http://eslint.org/docs/rules/jsx-quotes), 因此在双引号里包括像 `"don't"` 的属性值更容易输入.
193-
> HTML属性也是用双引号,所以JSX属性也遵循同样的语法.
192+
> 为什么? HTML属性也是用双引号, 因此JSX的属性也遵循此约定.
194193

195194
```jsx
196195
// bad
@@ -338,6 +337,35 @@
338337
/>
339338
))}
340339
```
340+
341+
- 对于所有非必须的属性,总是手动去定义`defaultProps`属性.
342+
343+
> 为什么? propTypes 可以作为模块的文档说明, 并且声明 defaultProps 的话意味着阅读代码的人不需要去假设一些默认值。更重要的是, 显示的声明默认属性可以让你的模块跳过属性类型的检查.
344+
345+
```jsx
346+
// bad
347+
function SFC({ foo, bar, children }) {
348+
return <div>{foo}{bar}{children}</div>;
349+
}
350+
SFC.propTypes = {
351+
foo: PropTypes.number.isRequired,
352+
bar: PropTypes.string,
353+
children: PropTypes.node,
354+
};
355+
356+
// good
357+
function SFC({ foo, bar }) {
358+
return <div>{foo}{bar}</div>;
359+
}
360+
SFC.propTypes = {
361+
foo: PropTypes.number.isRequired,
362+
bar: PropTypes.string,
363+
};
364+
SFC.defaultProps = {
365+
bar: '',
366+
children: null,
367+
};
368+
```
341369

342370
## Refs
343371

@@ -351,7 +379,7 @@
351379
352380
// good
353381
<Foo
354-
ref={ref => { this.myRef = ref; }}
382+
ref={(ref) => { this.myRef = ref; }}
355383
/>
356384
```
357385

0 commit comments

Comments
 (0)