关于

  • 基础规范

  • 组件结构

  • 命名规范

  • jsx 书写规范

  • eslint-plugin-react

关于

在代码的设计上,每个团队可能都有一定的代码规范和模式,好的代码规范能够提高代码的可读性便于协作沟通,好的模式能够上层设计上避免不必要的 bug 出现。本节会参考社区提供一些 React 的规范和优秀的设计模式。

基础规范

  1. 统一全部采用 Es6

  2. 组件文件名称采用大驼峰命名

组件结构

总体规则: stateless(Function) 优先于 Es6 Class 优先于 React.createClass;
书写规则: 规范组件内部方法的定义顺序

  • Es6 class 定义规范:

  1. static 方法

  2. constructor

  3. getChildContext

  4. componentWillMount

  5. componentDidMount

  6. componentWillReceiveProps

  7. shouldComponentUpdate

  8. componentWillUpdate

  9. componentDidUpdate

  10. componentWillUnmount

  11. clickHandlers + eventHandlers 如 onClickSubmit() 或 onChangeDescription()

  12. getter methods for render 如 getSelectReason() 或 getFooterContent()

  13. render methods 如 renderNavigation() 或 renderProfilePicture()

  14. render

以 Es6 Class 定义的组件为例;

const defaultProps = {
name: 'Guest'
};
const propTypes = {
name: React.PropTypes.string
};
class Person extends React.Component { // 构造函数
constructor (props) {
super(props);
// 定义 statethis.state = { smiling: false };
// 定义 eventHandlerthis.handleClick = this.handleClick.bind(this);
} // 生命周期方法
componentWillMount () {},
componentDidMount () {},
componentWillUnmount () {}, // getters and setters
get attr() {} // handlers
handleClick() {}, // render
renderChild() {},
render () {}, } /**
* 类变量定义
*/
Person.defaultProps = defaultProps; /**
* 统一都要定义 propTypes
* @type {Object}
*/
Person.propTypes = propTypes;

命名规范

  • 组件名称:大驼峰

  • 属性名称:小驼峰

  • 事件处理函数:handleSomething

  • 自定义事件属性名称:onSomething={this.handleSomething}

  • key: 不能使用数组 index ,构造或使用唯一的 id

  • 组件方法名称:避免使用下划线开头的命名

jsx 书写规范

  • 自闭合

// bad
<Foo className="stuff"></Foo> // good
<Foo className="stuff" />
  • 属性对齐

// bad
<Foo superLongParam="bar"
anotherSuperLongParam="baz" /> // good
<Foo
superLongParam="bar"
anotherSuperLongParam="baz"
/> // if props fit in one line then keep it on the same line
<Foo bar="bar" />
  • 返回

// bad
render() {
return <MyComponent className="long body" foo="bar"><MyChild /></MyComponent>;
} // good
render() {
return (
<MyComponent className="long body" foo="bar"><MyChild /></MyComponent>
);
} // good, when single line
render() {
const body = <div>hello</div>;
return <MyComponent>{body}</MyComponent>;
}

eslint-plugin-react

规范可以使用 eslint-plugin-react 插件来强制实施,规则和配置可查看 
https://github.com/yannickcr/eslint-plugin-react

更多 react 代码规范可参考 https://github.com/airbnb/javascript/tree/master/react

4.1 react 代码规范的更多相关文章

  1. React Native开发的一种代码规范:Eslint + FlowType

    [这篇随笔记录的很简单,没有涉及具体的Eslint规则解释以及FlowType的类型说明和使用等,只是链接了所需的若干文档] js开发很舒服,但是代码一多起来就参差不齐,难以阅读了.所以加上一些代码规 ...

  2. 在Git上如何强推代码规范

    引言 最近参加了“前端规范制定topic”小组,小组成员一起制定了html.css.js.es6.vue和react等规范,但规范制定好了怎么进行推广去强制执行呢,已知我们的项目都是用git做管理的, ...

  3. JavaScript必备:Google发布的JS代码规范(转)

    [翻译]关于Google发布的JS代码规范,你需要了解什么? 翻译 | WhiteYin 译文 | https://github.com/WhiteYin/translation/issues/10 ...

  4. 中小型前端团队代码规范工程化最佳实践 - ESLint

    前言 There are a thousand Hamlets in a thousand people's eyes. 一千个程序员,就有一千种代码风格.在前端开发中,有几个至今还在争论的代码风格差 ...

  5. eslint+prettier+husky+lint-staged 统一前端代码规范

    eslint+prettier+husky+lint-staged 统一前端代码规范 遵循编码规范和使用语法检测,可以很好的提高代码的可读性,可维护性,并有效的减少一些编码错误. 1.终极目标 团队中 ...

  6. iOS代码规范(OC和Swift)

    下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...

  7. 谈谈PHP代码规范

    [转] http://www.syyong.com/php/Talk-about-PHP-code-specification.html 我向往这样一个php世界,里面没有代码规范之争.你我都一样,都 ...

  8. 2016 正确 sublime安装PHPcs PHPcodesniffer代码规范提示插件,修正网上部分不详细描述

    对你有助请点赞,请顶,不好请踩------送人玫瑰,手留余香!-------------------14:37 2016/3/212016 正确 sublime安装PHPcs PHPcodesniff ...

  9. C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义

    类型判断符号: C#:object a;  if(a is int) { }  用 is 符号判断 Java:object a; if(a instanceof Integer) { } 用 inst ...

随机推荐

  1. python random模块随机取list中的某个值

    import random from random import randint ''' random.randint()随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值,pyth ...

  2. dp的刷表法和填表法

    dp的刷表法和填表法 参考: 动态规划刷表法 - acmer_xue的博客 - CSDN博客http://blog.csdn.net/qq_30241305/article/details/52198 ...

  3. MCS-51系列单片机和MCS-52系列单片机有何异同

    MSC-51:1,片内4K字节程序存储器:2,片内128字节数据存储器:3,片内2个16位硬件定时器/计数器.MSC-52: 1,片内8K字节程序存储器:2,片内256字节数据存储器:3,片内3个16 ...

  4. Centos7安装部署SonarQube7.9.1教程

    0.参考文档 LTS 7.9.1 新特性:https://www.sonarqube.org/sonarqube-7-9-lts/ JDK11 下载地址: 链接:https://pan.baidu.c ...

  5. CodeForces - 841B-Godsend-思维

    Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two pla ...

  6. C语言函数指针用法

    #include <stdio.h> #include <string.h> static void sayHello(); static void salute(); voi ...

  7. Vertical-Align,你应该知道的一切

    我们聊聊vertical-align.这个属性主要目的用于将相邻的文本与元素对齐.而实际上,verticle-algin可以在不同上下文中灵活地对齐元素,以及进行细粒度的控制,不必知道元素的大小.元素 ...

  8. [轉]C/C++中的volatile使用時機?

    不知各位對volatile(揮發性的)這個字陌不陌生? 我相信大家在一些程式或多或少都看 過這個字眼, 但是究竟要在何種場合用它呢?.當然一定是有需要, C/C++才會有這個保留字, 否則只是增加pr ...

  9. win10 虚拟机VMware 14中CentOS7文件共享

    一,环境 主机:win10 家庭版 软件:VMware 14 系统:CentOS 7 二,设置共享文件 右键虚拟机->选择设置 如图:创建共享文件 三,安装VMware Tools 然后进入ce ...

  10. elasticsearch启动常见问题

    原文:https://blog.csdn.net/qq_22211217/article/details/80740873 一.Exception in thread "main" ...