摘要:來自于中文編碼規(guī)范編碼規(guī)范算是最合理的編碼規(guī)范之一了基本規(guī)范每個(gè)文件只寫一個(gè)模塊但是多個(gè)無狀態(tài)模塊可以放在單個(gè)文件中推薦使用語法不要使用,除非從一個(gè)非的文件中初始化你的創(chuàng)建模塊如果你的模塊有內(nèi)部狀態(tài)或者是推薦使用而不是除非你有充足的理由
Airbnb React/JSX 編碼規(guī)范來自于Airbnb React/JSX 中文編碼規(guī)范
算是最合理的React/JSX編碼規(guī)范之一了
Basic Rules 基本規(guī)范
每個(gè)文件只寫一個(gè)模塊.
但是多個(gè)無狀態(tài)模塊可以放在單個(gè)文件中. eslint: react/no-multi-comp.
推薦使用JSX語法.
不要使用 React.createElement,除非從一個(gè)非JSX的文件中初始化你的app.
創(chuàng)建模塊Class vs React.createClass vs stateless
如果你的模塊有內(nèi)部狀態(tài)或者是refs, 推薦使用 class extends React.Component 而不是 React.createClass ,除非你有充足的理由來使用這些方法.
eslint: react/prefer-es6-class react/prefer-stateless-function
// bad const Listing = React.createClass({ // ... render() { return{this.state.hello}; } }); // good class Listing extends React.Component { // ... render() { return{this.state.hello}; } }
如果你的模塊沒有狀態(tài)或是沒有引用`refs`, 推薦使用普通函數(shù)(非箭頭函數(shù))而不是類:
// bad class Listing extends React.Component { render() { returnNaming 命名{this.props.hello}; } } // bad (relying on function name inference is discouraged) const Listing = ({ hello }) => ({hello}); // good function Listing({ hello }) { return{hello}; }
擴(kuò)展名: React模塊使用 .jsx 擴(kuò)展名.
?- 文件名: 文件名使用帕斯卡命名. 如, ReservationCard.jsx.
?- 引用命名: React模塊名使用帕斯卡命名,實(shí)例使用駱駝式命名. eslint: react/jsx-pascal-case
// bad import reservationCard from "./ReservationCard"; // good import ReservationCard from "./ReservationCard"; // bad const ReservationItem =; // good const reservationItem = ;
模塊命名: 模塊使用當(dāng)前文件名一樣的名稱. 比如 ReservationCard.jsx 應(yīng)該包含名為 ReservationCard的模塊. 但是,如果整個(gè)文件夾是一個(gè)模塊,使用 index.js作為入口文件,然后直接使用 index.js 或者文件夾名作為模塊的名稱:
// bad import Footer from "./Footer/Footer"; // bad import Footer from "./Footer/index"; // good import Footer from "./Footer";
高階模塊命名: 對(duì)于生成一個(gè)新的模塊,其中的模塊名 displayName 應(yīng)該為高階模塊名和傳入模塊名的組合. 例如, 高階模塊 withFoo(), 當(dāng)傳入一個(gè) Bar 模塊的時(shí)候, 生成的模塊名 displayName 應(yīng)該為 withFoo(Bar).
為什么?一個(gè)模塊的 displayName 可能會(huì)在開發(fā)者工具或者錯(cuò)誤信息中使用到,因此有一個(gè)能清楚的表達(dá)這層關(guān)系的值能幫助我們更好的理解模塊發(fā)生了什么,更好的Debug.
// bad export default function withFoo(WrappedComponent) { return function WithFoo(props) { return; } } // good export default function withFoo(WrappedComponent) { function WithFoo(props) { return ; } const wrappedComponentName = WrappedComponent.displayName || WrappedComponent.name || "Component"; WithFoo.displayName = `withFoo(${wrappedComponentName})`; return WithFoo; }
屬性命名: 避免使用DOM相關(guān)的屬性來用作其他的用途。
為什么?對(duì)于style 和 className這樣的屬性名,我們都會(huì)默認(rèn)它們代表一些特殊的含義,如元素的樣式,CSS class的名稱。在你的應(yīng)用中使用這些屬性來表示其他的含義會(huì)使你的代碼更難閱讀,更難維護(hù),并且可能會(huì)引起bug。
// badDeclaration 聲明模塊// good
不要使用 displayName 來命名React模塊,而是使用引用來命名模塊, 如 class 名稱.
// bad export default React.createClass({ displayName: "ReservationCard", // stuff goes here }); // good export default class ReservationCard extends React.Component { }Alignment 代碼對(duì)齊
遵循以下的JSX語法縮進(jìn)/格式. eslint: react/jsx-closing-bracket-location
// badQuotes 單引號(hào)還是雙引號(hào)// good, 有多行屬性的話, 新建一行關(guān)閉標(biāo)簽 // 若能在一行中顯示, 直接寫成一行 // 子元素按照常規(guī)方式縮進(jìn)
對(duì)于JSX屬性值總是使用雙引號(hào)("), 其他均使用單引號(hào)("). eslint: jsx-quotes
為什么? HTML屬性也是用雙引號(hào), 因此JSX的屬性也遵循此約定.
// badSpacing 空格// good // bad // good
總是在自動(dòng)關(guān)閉的標(biāo)簽前加一個(gè)空格,正常情況下也不需要換行. eslint: no-multi-spaces, react/jsx-space-before-closing
// bad// very bad // bad // good
不要在JSX {} 引用括號(hào)里兩邊加空格. eslint: react/jsx-curly-spacing
// badProps 屬性// good
JSX屬性名使用駱駝式風(fēng)格camelCase.
// bad// good
如果屬性值為 true, 可以直接省略. eslint: react/jsx-boolean-value
// bad// good
// good // good // good
不要在 alt 值里使用如 "image", "photo", or "picture"包括圖片含義這樣的詞, 中文也一樣. eslint: jsx-a11y/img-redundant-alt
為什么? 屏幕助讀器已經(jīng)把 img 標(biāo)簽標(biāo)注為圖片了, 所以沒有必要再在 alt 里說明了.
// bad // good
使用有效正確的 aria role屬性值 ARIA roles. eslint: jsx-a11y/aria-role
// bad - not an ARIA role // bad - abstract ARIA role // good
不要在標(biāo)簽上使用 accessKey 屬性. eslint: jsx-a11y/no-access-key
為什么? 屏幕助讀器在鍵盤快捷鍵與鍵盤命令時(shí)造成的不統(tǒng)一性會(huì)導(dǎo)致閱讀性更加復(fù)雜.
// bad // good
避免使用數(shù)組的index來作為屬性key的值,推薦使用唯一ID. (為什么?)
// bad {todos.map((todo, index) =>)} // good {todos.map(todo => ( ))}
對(duì)于所有非必須的屬性,總是手動(dòng)去定義defaultProps屬性.
為什么? propTypes 可以作為模塊的文檔說明, 并且聲明 defaultProps 的話意味著閱讀代碼的人不需要去假設(shè)一些默認(rèn)值。更重要的是, 顯示的聲明默認(rèn)屬性可以讓你的模塊跳過屬性類型的檢查.
// bad function SFC({ foo, bar, children }) { returnRefs{foo}{bar}{children}; } SFC.propTypes = { foo: PropTypes.number.isRequired, bar: PropTypes.string, children: PropTypes.node, }; // good function SFC({ foo, bar }) { return{foo}{bar}; } SFC.propTypes = { foo: PropTypes.number.isRequired, bar: PropTypes.string, }; SFC.defaultProps = { bar: "", children: null, };
總是在Refs里使用回調(diào)函數(shù). eslint: react/no-string-refs
// badParentheses 括號(hào)// good { this.myRef = ref; }} />
將多行的JSX標(biāo)簽寫在 ()里. eslint: react/wrap-multilines
// bad render() { returnTags 標(biāo)簽; } // good render() { return ( ); } // good, 單行可以不需要 render() { const body = hello; return{body} ; }
對(duì)于沒有子元素的標(biāo)簽來說總是自己關(guān)閉標(biāo)簽. eslint: react/self-closing-comp
// bad// good
如果模塊有多行的屬性, 關(guān)閉標(biāo)簽時(shí)新建一行. eslint: react/jsx-closing-bracket-location
// badMethods 函數(shù)// good
使用箭頭函數(shù)來獲取本地變量.
function ItemList(props) { return (
當(dāng)在 render() 里使用事件處理方法時(shí),提前在構(gòu)造函數(shù)里把 this 綁定上去. eslint: react/jsx-no-bind
為什么? 在每次 render 過程中, 再調(diào)用 bind 都會(huì)新建一個(gè)新的函數(shù),浪費(fèi)資源.
// bad class extends React.Component { onClickDiv() { // do stuff } render() { return } } // good class extends React.Component { constructor(props) { super(props); this.onClickDiv = this.onClickDiv.bind(this); } onClickDiv() { // do stuff } render() { return } }
在React模塊中,不要給所謂的私有函數(shù)添加 _ 前綴,本質(zhì)上它并不是私有的.
為什么?_ 下劃線前綴在某些語言中通常被用來表示私有變量或者函數(shù)。但是不像其他的一些語言,在JS中沒有原生支持所謂的私有變量,所有的變量函數(shù)都是共有的。盡管你的意圖是使它私有化,在之前加上下劃線并不會(huì)使這些變量私有化,并且所有的屬性(包括有下劃線前綴及沒有前綴的)都應(yīng)該被視為是共有的。了解更多詳情請(qǐng)查看Issue #1024, 和 #490 。
// bad React.createClass({ _onClickSubmit() { // do stuff }, // other stuff }); // good class extends React.Component { onClickSubmit() { // do stuff } // other stuff }
在 render 方法中總是確保 return 返回值. eslint: react/require-render-return
// bad render() { (); } // good render() { return (); }Ordering React 模塊生命周期
class extends React.Component 的生命周期函數(shù):
可選的 static 方法
constructor 構(gòu)造函數(shù)
getChildContext 獲取子元素內(nèi)容
componentWillMount 模塊渲染前
componentDidMount 模塊渲染后
componentWillReceiveProps 模塊將接受新的數(shù)據(jù)
shouldComponentUpdate 判斷模塊需不需要重新渲染
componentWillUpdate 上面的方法返回 true, 模塊將重新渲染
componentDidUpdate 模塊渲染結(jié)束
componentWillUnmount 模塊將從DOM中清除, 做一些清理任務(wù)
點(diǎn)擊回調(diào)或者事件處理器 如 onClickSubmit() 或 onChangeDescription()
render 里的 getter 方法 如 getSelectReason() 或 getFooterContent()
可選的 render 方法 如 renderNavigation() 或 renderProfilePicture()
render 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 {this.props.text} } } Link.propTypes = propTypes; Link.defaultProps = defaultProps; export default Link;
React.createClass 的生命周期函數(shù),與使用class稍有不同: eslint: react/sort-comp
displayName 設(shè)定模塊名稱
propTypes 設(shè)置屬性的類型
contextTypes 設(shè)置上下文類型
childContextTypes 設(shè)置子元素上下文類型
mixins 添加一些mixins
statics
defaultProps 設(shè)置默認(rèn)的屬性值
getDefaultProps 獲取默認(rèn)屬性值
getInitialState 或者初始狀態(tài)
getChildContext
componentWillMount
componentDidMount
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
componentDidUpdate
componentWillUnmount
clickHandlers or eventHandlers like onClickSubmit() or onChangeDescription()
getter methods for render like getSelectReason() or getFooterContent()
Optional render methods like renderNavigation() or renderProfilePicture()
render
isMounted不要再使用 isMounted. eslint: react/no-is-mounted
為什么? isMounted 反人類設(shè)計(jì)模式:(), 在 ES6 classes 中無法使用, 官方將在未來的版本里刪除此方法.
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/86864.html
摘要:編碼規(guī)范是獨(dú)角獸公司內(nèi)部的編碼規(guī)范,該項(xiàng)目是上很受歡迎的一個(gè)開源項(xiàng)目,在前端開發(fā)中使用廣泛,本文的配置規(guī)則就是以編碼規(guī)范和編碼規(guī)范作為基礎(chǔ)的。 更新時(shí)間:2019-01-22React.js create-react-app 項(xiàng)目 + VSCode 編輯器 + ESLint 代碼檢查工具 + Airbnb 編碼規(guī)范 前言 為什么要使用 ESLint 在項(xiàng)目開發(fā)過程中,編寫符合團(tuán)隊(duì)編碼規(guī)...
摘要:前言人是很懶惰的,你剛開始建立的一個(gè)規(guī)規(guī)整整的項(xiàng)目,可能一段時(shí)間過后,就回被你無數(shù)次的提交代碼弄得凌亂不堪。 前言 人是很懶惰的,你剛開始建立的一個(gè)規(guī)規(guī)整整的項(xiàng)目,可能一段時(shí)間過后,就回被你無數(shù)次的提交代碼弄得凌亂不堪。就算你能保證你的編碼風(fēng)格嚴(yán)謹(jǐn)統(tǒng)一,別人又該如何,每個(gè)人都有不一樣的編碼風(fēng)格,要保持統(tǒng)一,就要對(duì)項(xiàng)目進(jìn)行適當(dāng)?shù)墓芾?正文 接下來介紹個(gè)React項(xiàng)目簡單管理的一個(gè)實(shí)踐: ...
摘要:是一個(gè)代碼靜態(tài)檢查工具,可以檢查的語法錯(cuò)誤,提示潛在的,可以有效提高代碼質(zhì)量。維持前端團(tuán)隊(duì)高度一致的編碼風(fēng)格。 ESLint是一個(gè)JavaScript代碼靜態(tài)檢查工具,可以檢查JavaScript的語法錯(cuò)誤,提示潛在的bug,可以有效提高代碼質(zhì)量。維持前端團(tuán)隊(duì)高度一致的編碼風(fēng)格。ESLint不但提供一些默認(rèn)的規(guī)則,也提供用戶自定義規(guī)則來約束所寫的JavaScript代碼。 詳細(xì)的可以參...
摘要:有很強(qiáng)的自定義功能,插件庫很龐大,針對(duì)新語言插件更新很快,配合使用可以快速搭建適配語言的開發(fā)環(huán)境。該命令依賴于包。源目錄路徑輸出路徑把所有東西放入緩存中,每次只編譯修改過的文件發(fā)生錯(cuò)誤時(shí)不會(huì)中斷的流程,同時(shí)觸發(fā)消息提示在命令行中輸入運(yùn)行。 Sublime有很強(qiáng)的自定義功能,插件庫很龐大,針對(duì)新語言插件更新很快,配合使用可以快速搭建適配語言的開發(fā)環(huán)境。 1. babel-sublime ...
閱讀 989·2019-08-30 15:54
閱讀 1538·2019-08-30 15:54
閱讀 2460·2019-08-29 16:25
閱讀 1362·2019-08-29 15:24
閱讀 823·2019-08-29 12:11
閱讀 2567·2019-08-26 10:43
閱讀 1306·2019-08-26 10:40
閱讀 532·2019-08-23 16:24