React NativeReact-Native程序员

React 书写规范

2017-03-30  本文已影响251人  阿波罗程序猿

搜集并总结的一些React书写规范,帮助使用React开发者更好的构建项目中的代码。

基本规范

创建模块

// bad
const Going = React.createClass({
  // ...
  render() {
    return <div>{this.state.happy}</div>;
  }
});

// good
class Going extends React.Component {
  // ...
  render() {
    return <div>{this.state.happy}</div>;
  }
}
// bad
class Going extends React.Component {
  render() {
    return <div>{this.props.happy}</div>;
  }
}

// bad 
const Going = ({ happy }) => (
  <div>{happy}</div>
);

// good
function Going({ happy }) {
  return <div>{happy}</div>;
}

命名

// bad
import goOn from './GoOn';

// good
import GoOn from './GoOn';

// bad
const GoOn = <GoOn />;

// good
const goOn = <GoOn />;
// bad
<MyComponent className="some" />

// good
<MyComponent name="some" />

声明模块

// bad
export default React.createClass({
  //.......
});

// good
export default class GoOn extends React.Component {
  //.......
}

代码对齐

// bad
<GoOn aParam="bar"
     bParam="baz" />

// good, 有多行属性的话, 新建一行关闭标签
<GoOn
  aParam="bar"
  bParam="baz"
/>

// 若能在一行中显示, 直接写成一行(注意闭标签与属性之间的空格)
<GoOn aParam ="bar" />

// 子元素按照常规方式缩进
<GoOn
  aParam="bar"
  bParam="baz"
>
  <Something />
</GoOn>

单引号还是双引号

为什么? HTML属性也是用双引号, 因此JSX的属性也遵循此约定。

// bad
<GoOn happy='happy' />

// good
<GoOn happy="happy" />

// bad
<GoOn style={{ left: "20px" }} />

// good
<GoOn style={{ left: '20px' }} />

属性

// bad
<Person
  UserName="hello"
  phone_number={ 12345678 }
/>

// good
<Person
  userName="hello"
  phoneNumber={ 12345678 }
/>
// bad
<GoOn happy={ true } />

// good
<GoOn happy />

Refs

// bad
<GoOn
  ref="myRef"
/>

// good
<GoOn
  ref={(ref) => { this.myRef = ref; }}
/>

标签

// bad
<GoOn className="something"></Foo>

// good
<GoOn className="something" />

函数

function ItemList(props)  {
  return (
    <ul>
      {props.items.map((item, index) => (
        <Item
          key={item.key}
          onClick={() => doSomethingWith(item.name, index)}
        />
      ))}
    </ul>
  );
}

为什么? 在每次 render 过程中, 再调用 bind 都会新建一个新的函数,浪费资源。

// 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} />
  }
}

模块生命周期

  1. 可选的 static 方法
  2. constructor 构造函数
  3. componentWillMount 模块渲染前
  4. componentDidMount 模块渲染后
  5. componentWillReceiveProps 模块将接受新的数据
  6. shouldComponentUpdate 判断模块需不需要重新渲染
  7. componentWillUpdate 上面的方法返回 true, 模块将重新渲染
  8. componentDidUpdate 模块渲染结束
  9. componentWillUnmount 模块将从DOM中清除, 做一些清理任务
  10. render render() 方法
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;

为什么? propTypes 可以作为模块的文档说明, 并且声明 defaultProps 的话意味着阅读代码的人不需要去假设一些默认值。更重要的是, 显示的声明默认属性可以让你的模块跳过属性类型的检查。

上一篇 下一篇

猜你喜欢

热点阅读