Airbnb的编码规范是在业界非常流行的一套规范,而且它一直都在进化,推出最新技术的规范

用更合理的方式书写React和JSX

基本规则

  • 每个文件只包含一个React组件;

  • 始终使用 JSX 语法;
  • 不要使用 React.createElement方法,除非初始化 app 的文件不是 JSX 格式。

Class vs React.createClass vs stateless

  • 如果组件拥有内部的 state 或者 refs 的时,更推荐使用 class extends React.Component,除非你有一个非常好的理由要使用 mixin。 eslint: react/prefer-es6-class

    // bad
    const Listing = React.createClass({
    // ...
    render() {
    return <div>{this.state.hello}</div>;
    }
    }); // good
    class Listing extends React.Component {
    // ...
    render() {
    return <div>{this.state.hello}</div>;
    }
    }

      

    如果没有组件没有内部 state 或者 refs,那么普通函数 (不要使用箭头函数) 比类的写法更好:

    // bad
    class Listing extends React.Component {
    render() {
    return <div>{this.props.hello}</div>;
    }
    } // bad (因为箭头函数没有“name”属性)
    const Listing = ({ hello }) => (
    <div>{hello}</div>
    ); // good
    function Listing({ hello }) {
    return <div>{hello}</div>;
    }

命名

  • 扩展名:React 组件使用.jsx扩展名;
  • 文件名:文件名使用帕斯卡命名。 例如: ReservationCard.jsx。
  • 引用命名:React 组件使用帕斯卡命名,引用实例采用驼峰命名。 eslint: react/jsx-pascal-case

    // bad
    import reservationCard from './ReservationCard'; // good
    import ReservationCard from './ReservationCard'; // bad
    const ReservationItem = <ReservationCard />; // good
    const reservationItem = <ReservationCard />;

      

  • 组件命名:组件名称应该和文件名一致, 例如: ReservationCard.jsx 应该有一个ReservationCard的引用名称。 但是, 如果是在目录中的组件, 应该使用 index.jsx 作为文件名 并且使用文件夹名称作为组件名:

    // bad
    import Footer from './Footer/Footer'; // bad
    import Footer from './Footer/index'; // good
    import Footer from './Footer';

声明

  • 不要使用`displayName`属性来命名组件,应该使用类的引用名称。

    // bad
    export default React.createClass({
    displayName: 'ReservationCard',
    // stuff goes here
    }); // good
    export default class ReservationCard extends React.Component {
    }

对齐

  • 为 JSX 语法使用下列的对其方式。eslint: react/jsx-closing-bracket-location

    // bad
    <Foo superLongParam="bar"
    anotherSuperLongParam="baz" /> // good
    <Foo
    superLongParam="bar"
    anotherSuperLongParam="baz"
    />
    // 如果组件的属性可以放在一行就保持在当前一行中
    <Foo bar="bar" /> // 多行属性采用缩进
    <Foo
    superLongParam="bar"
    anotherSuperLongParam="baz"
    >
    <Quux />
    </Foo>

引号

  • JSX 的属性都采用双引号,其他的 JS 都使用单引号。eslint: jsx-quotes

    为什么这样做?JSX 属性 不能包含转义的引号, 所以当输入"don't"这类的缩写的时候用双引号会更方便。

    标准的 HTML 属性通常也会使用双引号,所以 JSX 属性也会遵守这样的约定。

     // bad
    <Foo bar='bar' /> // good
    <Foo bar="bar" /> // bad
    <Foo style={{ left: "20px" }} /> // good
    <Foo style={{ left: '20px' }} />

      

空格

  • 终始在自闭合标签前面添加一个空格。

    // bad
    <Foo/> // very bad
    <Foo /> // bad
    <Foo
    /> // good
    <Foo />

属性

  • 属性名称始终使用驼峰命名法。

    // bad
    <Foo
    UserName="hello"
    phone_number={12345678}
    /> // good
    <Foo
    userName="hello"
    phoneNumber={12345678}
    />
  • 当属性值等于true的时候,省略该属性的赋值。 eslint: react/jsx-boolean-value

    // bad
    <Foo
    hidden={true}
    /> // good
    <Foo
    hidden
    />

括号

  • 用括号包裹多行 JSX 标签。 eslint: react/wrap-multilines

    // 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: react/self-closing-comp

    // bad
    <Foo className="stuff"></Foo> // good
    <Foo className="stuff" />
  • 如果控件有多行属性,关闭标签要另起一行。 eslint: react/jsx-closing-bracket-location

    // bad
    <Foo
    bar="bar"
    baz="baz" /> // good
    <Foo
    bar="bar"
    baz="baz"
    />

方法

  • 在 render 方法中事件的回调函数,应该在构造函数中进行bind绑定。 eslint: react/jsx-no-bind

    为什么这样做? 在 render 方法中的 bind 调用每次调用 render 的时候都会创建一个全新的函数。

    // bad
    class extends React.Component {
    onClickDiv() {
    // do stuff
    } render() {
    return <div onClick={this.onClickDiv.bind(this)} />
    }
    } // good
    class extends React.Component {
    constructor(props) {
    super(props); this.onClickDiv = this.onClickDiv.bind(this);
    } onClickDiv() {
    // do stuff
    } render() {
    return <div onClick={this.onClickDiv} />
    }
    }
  • React 组件的内部方法命名不要使用下划线前缀。

    // bad
    React.createClass({
    _onClickSubmit() {
    // do stuff
    }, // other stuff
    }); // good
    class extends React.Component {
    onClickSubmit() {
    // do stuff
    } // other stuff
    }

排序

  • class extends React.Component的顺序:

  1. static静态方法
  2. constructor
  3. getChildContext
  4. componentWillMount
  5. componentDidMount
  6. componentWillReceiveProps
  7. shouldComponentUpdate
  8. componentWillUpdate
  9. componentDidUpdate
  10. componentWillUnmount
  11. 点击回调或者事件回调 比如 onClickSubmit() 或者 onChangeDescription()
  12. render函数中的 getter 方法 比如 getSelectReason() 或者 getFooterContent()
  13. 可选的 render 方法 比如 renderNavigation() 或者 renderProfilePicture()
  14. render

  • 怎样定义 propTypes, defaultProps, contextTypes等

    import React, { PropTypes } from 'react';
    
    const propTypes = {
    id: PropTypes.number.isRequired,
    url: PropTypes.string.isRequired,
    text: PropTypes.string,
    }; const defaultProps = {
    text: 'Hello World',
    }; class Link extends React.Component {
    static methodsAreOk() {
    return true;
    } render() {
    return <a href={this.props.url} data-id={this.props.id}>{this.props.text}</a>
    }
    } Link.propTypes = propTypes;
    Link.defaultProps = defaultProps; export default Link;
  • React.createClass的排序:eslint: react/sort-comp

  1. displayName
  2. propTypes
  3. contextTypes
  4. childContextTypes
  5. mixins
  6. statics
  7. defaultProps
  8. getDefaultProps
  9. getInitialState
  10. getChildContext
  11. componentWillMount
  12. componentDidMount
  13. componentWillReceiveProps
  14. shouldComponentUpdate
  15. componentWillUpdate
  16. componentDidUpdate
  17. componentWillUnmount
  18. 点击回调或者事件回调 比如 onClickSubmit() 或者 onChangeDescription()
  19. render函数中的 getter 方法 比如 getSelectReason() 或者 getFooterContent()
  20. 可选的 render 方法 比如 renderNavigation() 或者 renderProfilePicture()
  21. render

isMounted

扩展

【转】Airbnb React编码规范的更多相关文章

  1. eslint推荐编码规范和airbnb推荐编码规范

    Eslint规范 for 循环禁止使用无限循环(这个非默认推荐) // bad for (var i = 0; i < 10; i--) { } for (var i = 10; i >= ...

  2. react编码规范

    1.每个文件只写一个组件,但是多个无状态组件可以放在单个文件中: 2.有内部状态,方法或要对外暴露ref的组件,用类式组件: 3.无内部状态,方法或无需对外暴露ref的组件,用函数式组件: 4.有内部 ...

  3. Airbnb React/JSX 编码规范

    Airbnb React/JSX 编码规范 算是最合理的React/JSX编码规范之一了 内容目录 基本规范 Class vs React.createClass vs stateless 命名 声明 ...

  4. 【转】JavaScript 风格指南/编码规范(Airbnb公司版)

    原文转自:http://blog.jobbole.com/79484/ Airbnb 是一家位于美国旧金山的公司,本文是其内部的 JavaScript 风格指南/编码规范,在 Github 上有 11 ...

  5. JavaScript 编码规范(中文/Airbnb公司版)

    Airbnb 是一家位于美国旧金山的公司,本文是其内部的 JavaScript编码规范,写得比较全面,在 Github 上有 16,686 + Star,3,080 + fork,前端开发人员可参考. ...

  6. HTML/CSS/JS编码规范

    最近整理了一份HTML/CSS/JS编码规范,供大家参考.目录:一.HTML编码规范二.CSS编码规范三.JS编码规范 一.HTML编码规范 1. img标签要写alt属性 根据W3C标准,img标签 ...

  7. python编码规范、js编码规范及IDE的检查插件pylint/eslint等

    一.python规范 参考:https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/的风格规范和语 ...

  8. 4.1 react 代码规范

    关于 基础规范 组件结构 命名规范 jsx 书写规范 eslint-plugin-react 关于 在代码的设计上,每个团队可能都有一定的代码规范和模式,好的代码规范能够提高代码的可读性便于协作沟通, ...

  9. Android的编码规范

    一.Android编码规范 1.学会使用string.xml文件 在我看来,当一个文本信息出现的次数大于一次的时候就必须要使用string.xml 比如一个保存按钮 , 不规范写法: <Butt ...

随机推荐

  1. winddows 运行指令 (2)

    cmd.exe--------CMD命令提示符 chkdsk.exe-----Chkdsk磁盘检查 certmgr.msc----证书管理实用程序 calc-----------启动计算器 charm ...

  2. 支持向量机通俗导论(理解SVM的三层境界)

    原文链接:http://blog.csdn.net/v_july_v/article/details/7624837 作者:July.pluskid :致谢:白石.JerryLead 出处:结构之法算 ...

  3. rhel7修改网卡命名规则

    1步:当安装完红帽RHEL7系统安装完成,您的网卡命名是这样的. 第2步:请编辑网卡的配置文件 将”/etc/sysconfig/network-scripts/ifcfg-eno16777736“的 ...

  4. 从scrapy使用经历说开来

    关于scrapy这个Python框架,萌萌的官网这么介绍: An open source and collaborative framework for extracting the data you ...

  5. 4-pwd 打印当前工作目录

    pwd print name of current/working directory 打印当前工作目录 [语法]: pwd [选项] [参数] [功能介绍] pwd命令以绝对路径的方式显示用户当前工 ...

  6. Hibernate注解映射联合主键的三种主要方式

    今天在做项目的时候,一个中间表没有主键,所有在创建实体的时候也未加组件,结果报以下错误: org.springframework.beans.factory.BeanCreationException ...

  7. jsp内置对象作业3-application用户注册

    1,注册页面 zhuCe.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&qu ...

  8. Java算法-选择排序

    (转载出处) 选择排序的基本思想是遍历数组的过程中,以 i 代表当前需要排序的序号,则需要在剩余的 [i…n-1] 中找出其中的最小值,然后将找到的最小值与 i 指向的值进行交换.因为每一趟确定元素的 ...

  9. HTTP协议学习---(三)摘要认证

    Http 摘要认证 这个认证可以看做是基本认证的增强版本,使用随机数+密码进行md5,防止通过直接的分析密码MD5防止破解. 摘要访问认证最初由 RFC 2069 (HTTP的一个扩展:摘要访问认证) ...

  10. Linux下执行.sh文件

    Linux下执行.sh文件有两种情况: 一.直接./加上文件名.sh,如运行hello.sh为./hello.sh[hello.sh必须有x权限] 二.直接sh 加上文件名.sh,如运行hello.s ...