React代码开发规范

朱雀 2021-07-26 16:55 645阅读 0赞

前言

一般在团队开发中每个人的代码习惯都不太一样,这样就会导致代码风格不一致,以致于维护和修改bug的时候看别人的代码成为一种痛苦…
这种情况尤其在前端开发中尤为明显。因为关于前端的开发规范貌似也没有行业权威标准。这几天在网上看了下,基本上在开发中通过eslint进行约束,airbnb的标准貌似颇为推崇,今天稍微整理下,准备在日后开发中形成习惯。

基本规则

  1. 每个文件只包含一个React组件。eslint: react/no-multi-comp;(官方表示在无状态,或者Pure组件允许一个文件包含多个组件,但是我个人觉得一个文件只包含一个组件比较浅显易懂)
  2. 始终使用JSX语法;
  3. 不要始终React.createElement方法,除非初始化app的文件不是JSX格式;

Class vs React.createClass vs stateless

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

    // bad
    const Listing = React.createClass({
    // …
    render() {

    1. return <div>{this.state.hello}</div>;

    }
    });

    // good
    class Listing extends React.Component {
    // …
    render() {

    1. return <div>{this.state.hello}</div>;

    }
    }

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

  1. // bad
  2. class Listing extends React.Component {
  3. render() {
  4. return <div>{this.props.hello}</div>;
  5. }
  6. }
  7. // bad (因为箭头函数没有“name”属性)
  8. const Listing = ({ hello }) => (
  9. <div>{hello}</div>
  10. );
  11. // good
  12. function Listing({ hello }) {
  13. return <div>{hello}</div>;
  14. }

命名

  1. 拓展名:React组件使用.jsx扩展名;
  2. 文件名:文件名使用帕斯卡命名:HomePage.jsx
  3. 引用命名:React组件使用帕斯卡命名,引用实例采用驼峰式命名:eslint: react/jsx-pascal-case)(个人不喜欢这样,引用命名还是按照帕斯卡命名)

    // bad
    import reservationCard from ‘./ReservationCard’;

    // good
    import ReservationCard from ‘./ReservationCard’;

    // bad
    const ReservationItem = ;

    // good
    const reservationItem = ;

声明

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

  1. // bad
  2. export default React.createClass({
  3. displayName: 'ReservationCard',
  4. // stuff goes here
  5. });
  6. // good
  7. export default class ReservationCard extends React.Component {
  8. }

对齐

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

  1. // bad
  2. <Foo superLongParam="bar"
  3. anotherSuperLongParam="baz" />
  4. // good
  5. <Foo
  6. superLongParam="bar"
  7. anotherSuperLongParam="baz"
  8. />
  9. // 如果组件的属性可以放在一行就保持在当前一行中,(个人觉得如果只有一个属性就放在一行)
  10. <Foo bar="bar" />
  11. // 多行属性采用缩进
  12. <Foo
  13. superLongParam="bar"
  14. anotherSuperLongParam="baz"
  15. >
  16. <Quux />
  17. </Foo>

引号

JSX的属性都采用双引号,其他的JS都使用单引号:jsx-quotes
为什么这样做?JSX 属性 不能包含转义的引号, 所以当输入”don’t”这类的缩写的时候用双引号会更方便。

  1. // bad
  2. <Foo bar='bar' />
  3. // good
  4. <Foo bar="bar" />
  5. // bad
  6. <Foo style={
  7. { left: "20px" }} />
  8. // good
  9. <Foo style={
  10. { left: '20px' }} />

空格

始终在自闭和标签前添加一个空格。

  1. // bad
  2. <Foo/>
  3. // very bad
  4. <Foo />
  5. // bad
  6. <Foo
  7. />
  8. // good
  9. <Foo />

属性

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

    // bad

    // good

  2. 当属性值等于true的时候,省略该属性的赋值。eslint:react/jsx-boolean-value

    // bad

括号

使用括号包裹多行JSX标签,react/wrap-multilines

  1. // bad
  2. render() {
  3. return <MyComponent className="long body" foo="bar">
  4. <MyChild />
  5. </MyComponent>;
  6. }
  7. // good
  8. render() {
  9. return (
  10. <MyComponent className="long body" foo="bar">
  11. <MyChild />
  12. </MyComponent>
  13. );
  14. }
  15. // good, when single line
  16. render() {
  17. const body = <div>hello</div>;
  18. return <MyComponent>{body}</MyComponent>;
  19. }

标签

  1. 当标签没有子元素的时候,始终使用自闭合的标签:eslint: react/self-closing-comp

    // bad

    // good

  2. 如果控件有多行属性,关闭标签要另起一行。 eslint: react/jsx-closing-bracket-location

    // bad

    // good

方法

在 render 方法中事件的回调函数,应该在构造函数中进行bind绑定。 eslint: react/jsx-no-bind
为什么这样做? 在 render 方法中的 bind 调用每次调用 render 的时候都会创建一个全新的函数。

  1. // bad
  2. class extends React.Component {
  3. onClickDiv() {
  4. // do stuff
  5. }
  6. render() {
  7. return <div onClick={this.onClickDiv.bind(this)} />
  8. }
  9. }
  10. // good
  11. class extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.onClickDiv = this.onClickDiv.bind(this);
  15. }
  16. onClickDiv() {
  17. // do stuff
  18. }
  19. render() {
  20. return <div onClick={this.onClickDiv} />
  21. }
  22. }
  1. React 组件的内部方法命名不要使用下划线前缀。

    // bad
    React.createClass({
    _onClickSubmit() {

    1. // do stuff

    },

    // other stuff
    });

    // good
    class extends React.Component {
    onClickSubmit() {

    1. // 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()

可选的 render 方法 比如 renderNavigation() 或者 renderProfilePicture()

  1. render

怎么定义propTypes, defaultProps, contextTypes等

  1. import React, { PropTypes } from 'react';
  2. const propTypes = {
  3. id: PropTypes.number.isRequired,
  4. url: PropTypes.string.isRequired,
  5. text: PropTypes.string,
  6. };
  7. const defaultProps = {
  8. text: 'Hello World',
  9. };
  10. class Link extends React.Component {
  11. static methodsAreOk() {
  12. return true;
  13. }
  14. render() {
  15. return <a href={this.props.url} data-id={this.props.id}>{this.props.text}</a>
  16. }
  17. }
  18. Link.propTypes = propTypes;
  19. Link.defaultProps = defaultProps;
  20. export default Link;

关于这个开发规范差不多就这样吧,eslint的配置个人在研究下,下次再放出来

发表评论

表情:
评论列表 (有 0 条评论,645人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PHP开发代码规范

    1 编写目的 为了更好的提高技术部的工作效率,保证开发的有效性和合理性,并可最大程度的提高程序代码的可读性和可重复利用性,指定此规范。开发团队根据自己的实际情况,可以对本规

    相关 android开发 代码规范参考

    我们在开发项目的时候对于刚刚入门的同学来说代码的规范程度直接就可以看出你的项目开发经验,为此希望大家在开发的时候越来越规范自己的代码,做一个合格的程序员,为你的梦想冲

    相关 React代码开发规范

    前言 一般在团队开发中每个人的代码习惯都不太一样,这样就会导致代码风格不一致,以致于维护和修改bug的时候看别人的代码成为一种痛苦... 这种情况尤其在前端开发中尤为