@@ -9,6 +9,7 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
99
1010 1 . [ 基本规范] ( #basic-rules-基本规范 )
1111 1 . [ Class vs React.createClass vs stateless] ( #创建模块 )
12+ 1 . [ Mixins] ( #mixins-混入 )
1213 1 . [ 命名] ( #naming-命名 )
1314 1 . [ 声明模块] ( #declaration-声明模块 )
1415 1 . [ 代码对齐] ( #alignment-代码对齐 )
@@ -25,14 +26,15 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
2526## Basic Rules 基本规范
2627
2728 - 每个文件只写一个模块.
28- - 但是多个[ 无状态模块] ( https://facebook.github.io/react/docs/reusable-components.html#stateless-functions ) 可以放在单个文件中. eslint: [ ` react/no-multi-comp ` ] ( https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-multi-comp.md#ignorestateless ) .
29+ - 但是多个[ 无状态模块] ( https://facebook.github.io/react/docs/reusable-components.html#stateless-functions ) 可以放在单个文件中.
30+ eslint: [ ` react/no-multi-comp ` ] ( https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-multi-comp.md#ignorestateless ) .
2931 - 推荐使用JSX语法.
3032 - 不要使用 ` React.createElement ` ,除非从一个非JSX的文件中初始化你的app.
3133
3234## 创建模块
33- Class vs React.createClass vs stateless
35+ Class vs React.createClass vs stateless
3436
35- - 如果你的模块有内部状态或者是` refs ` , 推荐使用 ` class extends React.Component ` 而不是 ` React.createClass ` ,除非你有充足的理由来使用这些方法.
37+ - 如果你的模块有内部状态或者是` refs ` , 推荐使用 ` class extends React.Component ` 而不是 ` React.createClass ` ,除非你有充足的理由来使用这些方法.
3638 eslint: [ ` react/prefer-es6-class ` ] ( https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-es6-class.md ) [ ` react/prefer-stateless-function ` ] ( https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-stateless-function.md )
3739
3840 ``` jsx
@@ -74,11 +76,18 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
7476 }
7577 ` ` `
7678
79+ ## Mixins 混入
80+
81+ - [Do not use mixins](https: // facebook.github.io/react/blog/2016/07/13/mixins-considered-harmful.html).
82+
83+ > 为什么?混入引入了隐式依赖,导致命名冲突,导致组件越发复杂。大多数使用 Mixins 的案例,都能通过高阶组件或工具模块完成。
84+
7785## Naming 命名
7886
7987 - ** 扩展名** : React模块使用 ` .jsx` 扩展名.
8088 - ** 文件名** : 文件名使用帕斯卡命名. 如, ` ReservationCard.jsx` .
81- - ** 引用命名** : React模块名使用帕斯卡命名,实例使用骆驼式命名. eslint : [` react/jsx-pascal-case` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-pascal-case.md)
89+ - ** 引用命名** : React模块名使用帕斯卡命名,实例使用骆驼式命名.
90+ eslint: [` react/jsx-pascal-case` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-pascal-case.md)
8291
8392 ` ` ` jsx
8493 // bad
@@ -163,7 +172,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
163172
164173## Alignment 代码对齐
165174
166- - 遵循以下的JSX 语法缩进/ 格式. eslint : [` react/jsx-closing-bracket-location` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md)
175+ - 遵循以下的JSX 语法缩进/ 格式.
176+ eslint: [` react/jsx-closing-bracket-location` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md)
167177
168178 ` ` ` jsx
169179 // bad
@@ -190,7 +200,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
190200
191201## Quotes 单引号还是双引号
192202
193- - 对于JSX 属性值总是使用双引号(` "` ), 其他均使用单引号(` '` ). eslint : [` jsx-quotes` ](http: // eslint.org/docs/rules/jsx-quotes)
203+ - 对于JSX 属性值总是使用双引号(` "` ), 其他均使用单引号(` '` ).
204+ eslint: [` jsx-quotes` ](http: // eslint.org/docs/rules/jsx-quotes)
194205
195206 > 为什么? HTML 属性也是用双引号, 因此JSX 的属性也遵循此约定.
196207
@@ -210,7 +221,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
210221
211222## Spacing 空格
212223
213- - 总是在自动关闭的标签前加一个空格,正常情况下也不需要换行. eslint : [` no-multi-spaces` ](http: // eslint.org/docs/rules/no-multi-spaces), [`react/jsx-space-before-closing`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-space-before-closing.md)
224+ - 总是在自动关闭的标签前加一个空格,正常情况下也不需要换行.
225+ eslint: [` no-multi-spaces` ](http: // eslint.org/docs/rules/no-multi-spaces), [`react/jsx-space-before-closing`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-space-before-closing.md)
214226
215227 ` ` ` jsx
216228 // bad
@@ -227,7 +239,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
227239 <Foo />
228240 ` ` `
229241
230- - 不要在JSX ` {}` 引用括号里两边加空格. eslint : [` react/jsx-curly-spacing` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-curly-spacing.md)
242+ - 不要在JSX ` {}` 引用括号里两边加空格.
243+ eslint: [` react/jsx-curly-spacing` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-curly-spacing.md)
231244
232245 ` ` ` jsx
233246 // bad
@@ -255,7 +268,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
255268 />
256269 ` ` `
257270
258- - 如果属性值为 ` true` , 可以直接省略. eslint : [` react/jsx-boolean-value` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md)
271+ - 如果属性值为 ` true` , 可以直接省略.
272+ eslint: [` react/jsx-boolean-value` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md)
259273
260274 ` ` ` jsx
261275 // bad
@@ -269,7 +283,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
269283 />
270284 ` ` `
271285
272- - ` <img>` 标签总是添加 ` alt` 属性. 如果图片以presentation (感觉是以类似PPT 方式显示? )方式显示,` alt` 可为空, 或者` <img>` 要包含` role="presentation"` . eslint : [` jsx-a11y/img-has-alt` ](https: // github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/img-has-alt.md)
286+ - ` <img>` 标签总是添加 ` alt` 属性. 如果图片以presentation (感觉是以类似PPT 方式显示? )方式显示,` alt` 可为空, 或者` <img>` 要包含` role="presentation"` .
287+ eslint: [` jsx-a11y/img-has-alt` ](https: // github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/img-has-alt.md)
273288
274289 ` ` ` jsx
275290 // bad
@@ -310,7 +325,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
310325 <div role="button" />
311326 ` ` `
312327
313- - 不要在标签上使用 ` accessKey` 属性. eslint : [` jsx-a11y/no-access-key` ](https: // github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-access-key.md)
328+ - 不要在标签上使用 ` accessKey` 属性.
329+ eslint: [` jsx-a11y/no-access-key` ](https: // github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-access-key.md)
314330
315331 > 为什么? 屏幕助读器在键盘快捷键与键盘命令时造成的不统一性会导致阅读性更加复杂.
316332
@@ -340,8 +356,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
340356 />
341357 ))}
342358 ` ` `
343-
344- - 对于所有非必须的属性,总是手动去定义` defaultProps` 属性.
359+
360+ - 对于所有非必须的属性,总是手动去定义 ` defaultProps` 属性.
345361
346362 > 为什么? propTypes 可以作为模块的文档说明, 并且声明 defaultProps 的话意味着阅读代码的人不需要去假设一些默认值。更重要的是, 显示的声明默认属性可以让你的模块跳过属性类型的检查.
347363
@@ -363,6 +379,7 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
363379 SFC.propTypes = {
364380 foo: PropTypes.number.isRequired,
365381 bar: PropTypes.string,
382+ children: PropTypes.node,
366383 };
367384 SFC.defaultProps = {
368385 bar: '',
@@ -372,7 +389,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
372389
373390## Refs
374391
375- - 总是在Refs里使用回调函数. eslint : [` react/no-string-refs` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md)
392+ - 总是在Refs里使用回调函数.
393+ eslint: [` react/no-string-refs` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md)
376394
377395 ` ` ` jsx
378396 // bad
@@ -386,10 +404,10 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
386404 />
387405 ` ` `
388406
389-
390407## Parentheses 括号
391408
392- - 将多行的JSX 标签写在 ` ()` 里. eslint : [` react/wrap-multilines` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md)
409+ - 将多行的JSX 标签写在 ` ()` 里.
410+ eslint: [` react/wrap-multilines` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md)
393411
394412 ` ` ` jsx
395413 // bad
@@ -417,7 +435,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
417435
418436## Tags 标签
419437
420- - 对于没有子元素的标签来说总是自己关闭标签. eslint : [` react/self-closing-comp` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/self-closing-comp.md)
438+ - 对于没有子元素的标签来说总是自己关闭标签.
439+ eslint: [` react/self-closing-comp` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/self-closing-comp.md)
421440
422441 ` ` ` jsx
423442 // bad
@@ -427,7 +446,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
427446 <Foo className="stuff" />
428447 ` ` `
429448
430- - 如果模块有多行的属性, 关闭标签时新建一行. eslint : [` react/jsx-closing-bracket-location` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md)
449+ - 如果模块有多行的属性,关闭标签时新建一行.
450+ eslint: [` react/jsx-closing-bracket-location` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md)
431451
432452 ` ` ` jsx
433453 // bad
@@ -496,7 +516,9 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
496516 ` ` `
497517
498518 - 在React模块中,不要给所谓的私有函数添加 ` _` 前缀,本质上它并不是私有的.
499- > 为什么?` _` 下划线前缀在某些语言中通常被用来表示私有变量或者函数。但是不像其他的一些语言,在JS 中没有原生支持所谓的私有变量,所有的变量函数都是共有的。尽管你的意图是使它私有化,在之前加上下划线并不会使这些变量私有化,并且所有的属性(包括有下划线前缀及没有前缀的)都应该被视为是共有的。了解更多详情请查看Issue [#1024 ](https: // github.com/airbnb/javascript/issues/1024), 和 [#490](https://github.com/airbnb/javascript/issues/490) 。
519+
520+ > 为什么?` _` 下划线前缀在某些语言中通常被用来表示私有变量或者函数。但是不像其他的一些语言,在JS 中没有原生支持所谓的私有变量,所有的变量函数都是共有的。尽管你的意图是使它私有化,在之前加上下划线并不会使这些变量私有化,并且所有的属性(包括有下划线前缀及没有前缀的)都应该被视为是共有的。
521+ 了解更多详情请查看Issue [#1024 ](https: // github.com/airbnb/javascript/issues/1024), 和 [#490](https://github.com/airbnb/javascript/issues/490) 。
500522
501523 ` ` ` jsx
502524 // bad
@@ -518,7 +540,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
518540 }
519541 ` ` `
520542
521- - 在 ` render` 方法中总是确保 ` return` 返回值. eslint : [` react/require-render-return` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/require-render-return.md)
543+ - 在 ` render` 方法中总是确保 ` return` 返回值.
544+ eslint: [` react/require-render-return` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/require-render-return.md)
522545
523546 ` ` ` jsx
524547 // bad
@@ -546,7 +569,7 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
546569 1. ` componentWillUpdate` 上面的方法返回 ` true` , 模块将重新渲染
547570 1. ` componentDidUpdate` 模块渲染结束
548571 1. ` componentWillUnmount` 模块将从DOM 中清除, 做一些清理任务
549- 1. * 点击回调或者事件处理器 * 如 ` onClickSubmit()` 或 ` onChangeDescription()`
572+ 1. * 点击回调 clickHandlers 或者事件处理器 eventHandlers * 如 ` onClickSubmit()` 或 ` onChangeDescription()`
550573 1. * ` render` 里的 getter 方法* 如 ` getSelectReason()` 或 ` getFooterContent()`
551574 1. * 可选的 render 方法* 如 ` renderNavigation()` 或 ` renderProfilePicture()`
552575 1. ` render` render () 方法
@@ -582,7 +605,8 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
582605 export default Link;
583606 ` ` `
584607
585- - ` React.createClass` 的生命周期函数,与使用class 稍有不同: eslint: [` react/sort-comp` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md)
608+ - ` React.createClass` 的生命周期函数,与使用class 稍有不同:
609+ eslint: [` react/sort-comp` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md)
586610
587611 1. ` displayName` 设定模块名称
588612 1. ` propTypes` 设置属性的类型
@@ -608,10 +632,25 @@ source: https://github.com/JasonBoy/javascript/tree/master/react
608632
609633## isMounted
610634
611- - 不要再使用 ` isMounted` . eslint : [` react/no-is-mounted` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-is-mounted.md)
635+ - 不要再使用 ` isMounted` .
636+ eslint: [` react/no-is-mounted` ](https: // github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-is-mounted.md)
612637
613- > 为什么? [` isMounted` 反人类设计模式: ()][anti- pattern], 在 ES6 classes 中无法使用, 官方将在未来的版本里删除此方法.
638+ > 为什么? [` isMounted` 反人类设计模式: ()][anti- pattern], 在 ES6 classes 中无法使用,官方将在未来的版本里删除此方法.
614639
615640 [anti- pattern]: https: // facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html
616641
617642** [⬆ 回到顶部](#内容目录)**
643+
644+ ## Translation 翻译
645+
646+ This JSX / React style guide is also available in other languages:
647+
648+ - ! [cn](https: // raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese (Simplified)**: [JasonBoy/javascript](https://github.com/JasonBoy/javascript/tree/master/react)
649+ - ! [pl](https: // raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Poland.png) **Polish**: [pietraszekl/javascript](https://github.com/pietraszekl/javascript/tree/master/react)
650+ - ! [kr](https: // raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/South-Korea.png) **Korean**: [apple77y/javascript](https://github.com/apple77y/javascript/tree/master/react)
651+ - ! [Br](https: // raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Brazil.png) **Portuguese**: [ronal2do/javascript](https://github.com/ronal2do/airbnb-react-styleguide)
652+ - ! [jp](https: // raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Japan.png) **Japanese**: [mitsuruog/javascript-style-guide](https://github.com/mitsuruog/javascript-style-guide/tree/master/react)
653+ - ! [es](https: // raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Spain.png) **Español**: [agrcrobles/javascript](https://github.com/agrcrobles/javascript/tree/master/react)
654+ - ! [ua](https: // raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Ukraine.png) **Ukrainian**: [ivanzusko/javascript](https://github.com/ivanzusko/javascript/tree/master/react)
655+
656+ ** [⬆ 回到顶部](#内容目录)**
0 commit comments