Airbnb - 你非常值得學習的 React-JSX 編碼風格指南

作者:jiahao-c 譯
https://github.com/jiahao-c/javascript/tree/master/react

Airbnb React/JSX 風格指南

此風格指南主要基於目前流行的 JavaScript 標準。對於項目中是否應允許使用一些慣例(如 async/await,靜態 class 屬性等)的問題,應視具體情況而定。目前,本指南不包含並且不推薦使用任何第三階段 * 前的功能。

譯者注:第三階段指 TC39 流程中的 Stage 3 - Candidate。每個新的 ECMAScript 提案從起草到完成共分五個階段。

內容目錄

  1. 基本規範

  2. Class、React.createClass 與 stateless

  3. Mixins(混入)

  4. 命名

  5. 聲明模塊

  6. 代碼對齊

  7. 單引號還是雙引號

  8. 空格

  9. 屬性

  10. Refs 引用

  11. 括號

  12. 標籤

  13. 函數 / 方法

  14. 模塊生命週期

  15. isMounted

Basic Rules 基本規範

創建模塊

Class vs React.createClass vs stateless

Mixins

爲什麼? Mixins 會增加隱式的依賴,導致命名衝突,並且會以雪球式增加複雜度。在大多數情況下 Mixins 可以被更好的方法替代,如:組件化,高階組件,工具模塊等。

Naming 命名

Declaration 聲明模塊

Alignment 代碼對齊

Quotes 單引號還是雙引號

Spacing 空格

Props 屬性

爲什麼? 屏幕助讀器在鍵盤快捷鍵與鍵盤命令時造成的不統一性會導致閱讀性更加複雜.

// bad
<section accessKey="h" />

// good
<section />
// bad
{todos.map((todo, index) =>
  <Todo
    {...todo}
    key={index}
  />
)}

// good
{todos.map(todo =(
  <Todo
    {...todo}
    key={todo.id}
  />
))}

爲什麼? propTypes 可以作爲模塊的文檔說明, 並且聲明 defaultProps 的話意味着閱讀代碼的人不需要去假設一些默認值。更重要的是, 顯示的聲明默認屬性可以讓你的模塊跳過屬性類型的檢查.

// bad
function SFC({ foo, bar, children }) {
  return <section>{foo}{bar}{children}</section>;
}
SFC.propTypes = {
  foo: PropTypes.number.isRequired,
  bar: PropTypes.string,
  children: PropTypes.node,
};

// good
function SFC({ foo, bar, children }) {
  return <section>{foo}{bar}{children}</section>;
}
SFC.propTypes = {
  foo: PropTypes.number.isRequired,
  bar: PropTypes.string,
  children: PropTypes.node,
};
SFC.defaultProps = {
  bar: '',
  children: null,
};

爲什麼? 除非你很想傳遞一些不必要的屬性。對於 React v15.6.1 和更早的版本,你可以給 DOM 傳遞一些無效的 HTML 屬性

例外情況:

function HOC(WrappedComponent) {
  return class Proxy extends React.Component {
    Proxy.propTypes = {
      text: PropTypes.string,
      isLoading: PropTypes.bool
    };

    render() {
      return <WrappedComponent {...this.props} />
    }
  }
}
export default function Foo {
  const props = {
    text: '',
    isPublished: false
  }

  return (<section {...props} />);
}

特別提醒:儘可能地篩選出不必要的屬性。同時,使用 prop-types-exact 來預防問題出現。

// bad
render() {
  const { irrelevantProp, ...relevantProps  } = this.props;
  return <WrappedComponent {...this.props} />
}

// good
render() {
  const { irrelevantProp, ...relevantProps  } = this.props;
  return <WrappedComponent {...relevantProps} />
}

Refs

Parentheses 括號

Tags 標籤

Methods 函數

Ordering React 模塊生命週期

  1. 可選的 static 方法

  2. constructor 構造函數

  3. getChildContext 獲取子元素內容

  4. componentWillMount 模塊渲染前

  5. componentDidMount 模塊渲染後

  6. componentWillReceiveProps 模塊將接受新的數據

  7. shouldComponentUpdate 判斷模塊需不需要重新渲染

  8. componentWillUpdate 上面的方法返回 true, 模塊將重新渲染

  9. componentDidUpdate 模塊渲染結束

  10. componentWillUnmount 模塊將從 DOM 中清除, 做一些清理任務

  11. 點擊回調或者事件處理器onClickSubmit()onChangeDescription()

  12. render 裏的 getter 方法getSelectReason()getFooterContent()

  13. 可選的 render 方法renderNavigation()renderProfilePicture()

  14. render render() 方法

  1. displayName 設定模塊名稱

  2. propTypes 設置屬性的類型

  3. contextTypes 設置上下文類型

  4. childContextTypes 設置子元素上下文類型

  5. mixins 添加一些 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. clickHandlers or eventHandlers like onClickSubmit() or onChangeDescription()

  19. getter methods for render like getSelectReason() or getFooterContent()

  20. Optional render methods like renderNavigation() or renderProfilePicture()

  21. render

isMounted

爲什麼? isMounted 反人類設計模式:(), 在 ES6 classes 中無法使用, 官方將在未來的版本里刪除此方法.

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/e7YUdTS73rM6QN9WrpjUkg