Skip to content

Commit 89ae001

Browse files
committed
update react style, sync with upstream
1 parent e30a9d0 commit 89ae001

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

react/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1. [单引号还是双引号](#quotes-单引号还是双引号)
1313
1. [空格](#spacing-空格)
1414
1. [属性](#props-属性)
15+
1. [Refs引用](#refs)
1516
1. [括号](#parentheses-括号)
1617
1. [标签](#tags-标签)
1718
1. [函数/方法](#methods-函数)
@@ -102,7 +103,32 @@
102103
// good
103104
import Footer from './Footer';
104105
```
106+
- **高阶模块命名**: 对于生成一个新的模块,其中的模块名 `displayName` 应该为高阶模块名和传入模块名的组合. 例如, 高阶模块 `withFoo()`, 当传入一个 `Bar` 模块的时候, 生成的模块名 `displayName` 应该为 `withFoo(Bar)`.
105107

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+
```
106132
## Declaration 声明模块
107133

108134
- 不要使用 `displayName` 来命名React模块,而是使用引用来命名模块, 如 class 名称.
@@ -300,6 +326,23 @@
300326
))}
301327
```
302328

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+
303346
## Parentheses 括号
304347

305348
- 将多行的JSX标签写在 `()`里. eslint: [`react/wrap-multilines`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md)

0 commit comments

Comments
 (0)