React 代码规范

作者:陈学家 链接:https://zhuanlan.zhihu.com/p/21458464
来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

内容目录:

  • 关于
  • 基础规范
  • 组件结构
  • 命名规范
  • 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);
     // 定义 state 
    this.state = { smiling: false }; 
    // 定义 eventHandler 
    this.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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近看了一本关于学习方法论的书,强调了记笔记和坚持的重要性。这几天也刚好在学习React,所以我打算每天坚持一篇R...
    gaoer1938阅读 1,738评论 0 5
  • 原教程内容详见精益 React 学习指南,这只是我在学习过程中的一些阅读笔记,个人觉得该教程讲解深入浅出,比目前大...
    leonaxiong阅读 2,862评论 1 18
  • 本笔记基于React官方文档,当前React版本号为15.4.0。 1. 安装 1.1 尝试 开始之前可以先去co...
    Awey阅读 7,822评论 14 128
  • GUIDS 第一章 为什么使用React? React 一个提供了用户接口的JavaScript库。 诞生于Fac...
    jplyue阅读 3,611评论 1 11
  • 阅读本文大概需要3分钟~ 01 中印掷石块视频曝光 15日上午,中印士兵在拉达克地区的班公湖附近发生了对峙,双方互...
    小猪编阅读 432评论 0 0