生活随笔
收集整理的這篇文章主要介紹了
ReactNaitve代码规范和工具格式化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.代碼格式規范
1.1 命名
命名優先使用英文全拼,過長可適當使用英文縮寫,命名應少于20個字符
order order_page
HomeView
. js
組件名:組件名稱應該和文件名一致,使用駝峰命名且首字母大寫
export default class ProjectDetail extends Component { }
import demoPgae
from './DemoPage'
import DemoPage
from './DemoPage'
函數名:使用小寫駝峰命名,對于渲染組件型使用renderXxx( )
onRefresh = ( ) => { }
renderItem = ( ) => { }
1.2 對齊
// bad
< Foo superLongParam = " bar" anotherSuperLongParam = " baz" /> // good
< FoosuperLongParam = " bar" anotherSuperLongParam = " baz"
/> // 如果一行能擺下props,那就擺在一行
< Foo bar = " bar" /> // 子組件照常縮進
< FoosuperLongParam = " bar" anotherSuperLongParam = " baz"
> < Spazz />
</ Foo>
1.3 引號
對于JSX的字符串屬性使用雙引號("),其他情況下使用單引號。
// bad
< Foo bar = ' bar' /> // good
< Foo bar = " bar" /> // bad
< Foo style = {{ left: "20px" }} /> // good
< Foo style = {{ left: '20px' }} />
1.4 空格
// bad
< Foo/> // bad
< Foo/> // good
< Foo />
1.5 state/props
對于多個單詞組成的pros,使用駝峰命名法。不使用下劃線或連接線。
// bad
< FooUserName = " hello" phone_number = {12345678}
/> // good
< FoouserName = " hello" phoneNumber = {12345678}
/>
讀取state和props時,使用const與解構,必要時可使用let。不使用var。
var userName
= this . props
. userName
;
let checked
= this . state
. checked
;
const { userName
, age
, sex
} = this . props
;
const { checked
} = this . state
;
1.6 括號
render ( ) { return < MyComponent className
= "long body" foo
= "bar" > < MyChild
/ > < / MyComponent
> ;
}
render ( ) { return ( < MyComponent className
= "long body" foo
= "bar" > < MyChild
/ > < / MyComponent
> ) ;
}
render ( ) { const body
= < Text
> hello
< / Text
> return < MyComponent
> { body
} < / MyComponent
>
}
1.7 標簽
// bad
< Foo className = " stuff" > </ Foo> // good
< Foo className = " stuff" />
// bad
< Foobar = " bar" baz = " baz" /> // good
< Foobar = " bar" baz = " baz"
/>
1.8 組件內部方法聲明順序
原則上按如下順序排列ReactNative組件的各個方法(生命周期):
靜態屬性和方法 生命周期方法: constructor getDefaultProps getInitialState, getChildContext getDerivedStateFromProps componentWillMount UNSAFE_componentWillMount componentDidMount componentWillReceiveProps UNSAFE_componentWillReceiveProps shouldComponentUpdate componentWillUpdate UNSAFE_componentWillUpdate getSnapshotBeforeUpdate componentDidUpdate componentDidCatch componentWillUnmount 自定義方法 渲染組件的方法 renderXxx() render方法 組件外部最后寫StyleSheet
export default class DemoComponent extends Component { constructor ( props ) { super ( props
) this . state
= { } this . initVal ( ) } componentWillMount ( ) { } initVal = ( ) => { } loadData = ( ) => { } renderContent = ( ) => { return ( < View
/ > ) } render ( ) { return ( < View style
= { ResStyles
. container
} > { this . renderContent ( ) } < / View
> ) }
} const styles
= StyleSheet
. create ( { container
: { } ,
} ) ;
2.格式化工具
項目使用 ESLint + Prettier 來統一代碼格式
ESLint:檢測出代碼中的潛在問題,提高代碼質量 Prettier:統一代碼風格
為什么要把 ESLint 的 代碼格式規范 部分用 Prettier 取代?
代碼規范比 ESLint 的 Airbnb、Standard 更好更先進。 比 ESLint 提供更少的代碼風格規則配置項,終極目的是結束關于代碼風格的爭論。 比 ESLint 支持更多的語言
使用步驟:
在VSCode中下載安裝 ESLint、Prettier 插件,它們的配置文件.eslintrc.js和.prettierrc.js配置好,跟隨項目保存在git倉庫里
module
. exports
= { parser
: 'babel-eslint' , env
: { es6
: true , } , extends : [ 'standard' , 'plugin:prettier/recommended' ] , globals
: { Atomics
: 'readonly' , SharedArrayBuffer
: 'readonly' , } , parserOptions
: { ecmaFeatures
: { jsx
: true , legacyDecorators
: true , } , ecmaVersion
: 2018 , sourceType
: 'module' , } , plugins
: [ 'react' ] , rules
: { 'no-unused-vars' : 0 , 'react/sort-comp' : 1 , camelcase
: 2 , 'prettier/prettier' : [ 'error' , { printWidth
: 120 , } , ] , } ,
}
module
. exports
= { printWidth
: 120 , tabWidth
: 2 , useTabs
: false , singleQuote
: true , semi
: false , trailingComma
: 'all' , bracketSpacing
: true , wrapAttributes
: false , sortAttributes
: true , jsxBracketSameLine
: false , arrowParens
: 'avoid' , endOfLine
: 'auto' ,
}
在VSCode的settings.json文件中添加配置,開啟保存自動格式化、自動修改代碼錯誤、自動優化import引用 功能
"editor.defaultFormatter" : "esbenp.prettier-vscode" , "editor.codeActionsOnSave" : { "source.organizeImports" : true , "source.fixAll" : true } , "editor.formatOnSave" : true , "eslint.autoFixOnSave" : true , "window.zoomLevel" : 1
3.代碼書寫規范
3.1 尺寸單位
UI組件的尺寸設置使用內部封裝的dp(30)而不是30 px。
目前項目設計基準像素為1334 * 750,可將UI設計圖寬度調整成750方便使用。
3.2 函數的使用
函數作為 props 傳遞給子組件時,優先使用箭頭函數,可省去在constructor中綁定this指向
函數無參數時,應直接引用函數,不要多此一舉再嵌套一層
handleClick = ( ) => { console
. log ( 'Click happened' ) } render ( ) { return < Button onClick
= { this . handleClick
} > Click Me
< / Button
> } render ( ) { return < Button onClick
= { ( ) => this . handleClick ( ) } > Click Me
< / Button
> }
render ( ) { return < Button onClick
= { ( ) => this . handleClick ( id
) } > Click Me
< / Button
> }
3.3 JSX中的條件判斷
render ( ) { return ( < View
> { applyStatus
=== '1' ? < Tab1
/ > : < Tab2
/ > } < / View
> ) }
render ( ) { return ( < View
> { applyStatus
=== '1' && < Tab1
/ > } < / View
> ) }
復雜的條件場景,使用多個三目表達式會降低可讀性,推薦拆分成方法
renderComponent = ( flag, flag2, flag3, flag4, flag5 ) => { if ( flag
&& flag2
&& ! flag3
) { if ( flag4
) { return < Text
> Blah
< / Text
> } else if ( flag5
) { return < Text
> Meh
< / Text
> } else { return < Text
> Herp
< / Text
> } } else { return < Text
> Derp
< / Text
> } } render ( ) { return < View
> { this . renderComponent ( ... ) } < / View
> }
總結
以上是生活随笔 為你收集整理的ReactNaitve代码规范和工具格式化 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。