File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 12
12
1 . [ 单引号还是双引号] ( #quotes-单引号还是双引号 )
13
13
1 . [ 空格] ( #spacing-空格 )
14
14
1 . [ 属性] ( #props-属性 )
15
+ 1 . [ Refs引用] ( #refs )
15
16
1 . [ 括号] ( #parentheses-括号 )
16
17
1 . [ 标签] ( #tags-标签 )
17
18
1 . [ 函数/方法] ( #methods-函数 )
102
103
// good
103
104
import Footer from './Footer';
104
105
` ` `
106
+ - ** 高阶模块命名** : 对于生成一个新的模块,其中的模块名 ` displayName` 应该为高阶模块名和传入模块名的组合. 例如, 高阶模块 ` withFoo()` , 当传入一个 ` Bar` 模块的时候, 生成的模块名 ` displayName` 应该为 ` withFoo(Bar)` .
105
107
108
+ > 为什么?一个模块的 ` displayName` 可能会在开发者工具或者错误信息中使用到,因此有一个能清楚的表达这层关系的值能帮助我们更好的理解模块发生了什么,更好的Debug.
109
+
110
+ ` ` ` jsx
111
+ // bad
112
+ export default function withFoo(WrappedComponent) {
113
+ return function WithFoo(props) {
114
+ return <WrappedComponent {...props} foo />;
115
+ }
116
+ }
117
+
118
+ // good
119
+ export default function withFoo(WrappedComponent) {
120
+ function WithFoo(props) {
121
+ return <WrappedComponent {...props} foo />;
122
+ }
123
+
124
+ const wrappedComponentName = WrappedComponent.displayName
125
+ || WrappedComponent.name
126
+ || 'Component';
127
+
128
+ WithFoo.displayName = ` withFoo (${wrappedComponentName})` ;
129
+ return WithFoo;
130
+ }
131
+ ` ` `
106
132
## Declaration 声明模块
107
133
108
134
- 不要使用 ` displayName` 来命名React模块,而是使用引用来命名模块, 如 class 名称.
300
326
))}
301
327
` ` `
302
328
329
+ ## Refs
330
+
331
+ - 总是在Refs里使用回调函数. eslint : [` react/no-string-refs` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md)
332
+
333
+ ` ` ` jsx
334
+ // bad
335
+ <Foo
336
+ ref="myRef"
337
+ />
338
+
339
+ // good
340
+ <Foo
341
+ ref={ref => { this.myRef = ref; }}
342
+ />
343
+ ` ` `
344
+
345
+
303
346
## Parentheses 括号
304
347
305
348
- 将多行的JSX 标签写在 ` ()` 里. eslint : [` react/wrap-multilines` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md)
You can’t perform that action at this time.
0 commit comments