File tree Expand file tree Collapse file tree 1 file changed +32
-4
lines changed Expand file tree Collapse file tree 1 file changed +32
-4
lines changed Original file line number Diff line number Diff line change 187
187
188
188
## Quotes 单引号还是双引号
189
189
190
- - 对于JSX 属性值总是使用双引号(` "` ), 其他均使用单引号. eslint : [` jsx-quotes` ](http: // eslint.org/docs/rules/jsx-quotes)
190
+ - 对于JSX 属性值总是使用双引号(` "` ), 其他均使用单引号( ` ' ` ) . eslint : [` jsx-quotes` ](http: // eslint.org/docs/rules/jsx-quotes)
191
191
192
- > 为什么? JSX 属性 [不能包括转译的引号](http: // eslint.org/docs/rules/jsx-quotes), 因此在双引号里包括像 `"don't"` 的属性值更容易输入.
193
- > HTML 属性也是用双引号,所以JSX 属性也遵循同样的语法.
192
+ > 为什么? HTML 属性也是用双引号, 因此JSX 的属性也遵循此约定.
194
193
195
194
` ` ` jsx
196
195
// bad
338
337
/>
339
338
))}
340
339
` ` `
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
+ ` ` `
341
369
342
370
## Refs
343
371
351
379
352
380
// good
353
381
<Foo
354
- ref={ref => { this.myRef = ref; }}
382
+ ref={( ref) => { this.myRef = ref; }}
355
383
/>
356
384
` ` `
357
385
You can’t perform that action at this time.
0 commit comments