- Hands-On Design Patterns with React Native
- Mateusz Grzesiukiewicz
- 358字
- 2021-08-13 15:13:03
Airbnb React style guide rules
The Airbnb React style guide defines tens of well-thought-out rules. This is a great resource and a foundation for your next React project. I highly recommend looking into them. You can find the Airbnb React style guide at https://github.com/airbnb/javascript/tree/master/react.
However, everyone should find their own style. Mine just adapts a few things from the Airbnb:
- comma-dangle: Airbnb advises that you leave a trailing comma at the end of array multiline elements, lists, or object multiline key-value lists. This is not what I'm used to. I prefer the JSON style, which does not leave a trailing comma:
// My preference const hero = { firstName: 'Dana', lastName: 'Scully' }; const heroes = [ 'Batman', 'Superman' ]; // Airbnb style guide const hero = { firstName: 'Dana', lastName: 'Scully', }; const heroes = [ 'Batman', 'Superman', ];
- react/jsx-filename-extension: In my opinion, this rule should be changed in the style guide. It tries to convince you to use the .jsx extension for files using JSX. I don't agree with this. I would like to quote Dan Abramov's comment on this matter:
"The distinction between .js and .jsx files was useful before Babel, but it’s not that useful anymore.
There are other syntax extensions (for example, Flow). What would you call a JS file that uses Flow? .flow.js? What about a JSX file that uses Flow? .flow.jsx? What about some other experimental syntax? .flow.stage-1.jsx?
Most editors are configurable, so you can tell them to use a JSX-capable syntax scheme for .js files. Since JSX (or Flow) are strict supersets of JS, I don’t see this as an issue."
– Dan Abramov: https://github.com/facebook/create-react-app/issues/87#issuecomment-234627904.
There are other syntax extensions (for example, Flow). What would you call a JS file that uses Flow? .flow.js? What about a JSX file that uses Flow? .flow.jsx? What about some other experimental syntax? .flow.stage-1.jsx?
Most editors are configurable, so you can tell them to use a JSX-capable syntax scheme for .js files. Since JSX (or Flow) are strict supersets of JS, I don’t see this as an issue."
– Dan Abramov: https://github.com/facebook/create-react-app/issues/87#issuecomment-234627904.
- no-use-before-define: This is a smart rule. It prevents you from using variables and functions that are defined later, besides the fact that the JavaScript hoisting mechanism lets you to do so. However, I like to put my StyleSheets in the bottom on every component file. Hence, I have relaxed this rule to allow usage of variables before their definition.
I also prefer an indentation of four spaces for clarity when I copy snippets into this book.
推薦閱讀
- 演進(jìn)式架構(gòu)(原書第2版)
- Visual C++程序設(shè)計(jì)學(xué)習(xí)筆記
- Python程序設(shè)計(jì)(第3版)
- Apache Spark Graph Processing
- Java Web程序設(shè)計(jì)
- Hands-On Natural Language Processing with Python
- Kotlin編程實(shí)戰(zhàn):創(chuàng)建優(yōu)雅、富于表現(xiàn)力和高性能的JVM與Android應(yīng)用程序
- 動(dòng)手學(xué)數(shù)據(jù)結(jié)構(gòu)與算法
- Buildbox 2.x Game Development
- 跟戴銘學(xué)iOS編程:理順核心知識(shí)點(diǎn)
- 物聯(lián)網(wǎng)系統(tǒng)架構(gòu)設(shè)計(jì)與邊緣計(jì)算(原書第2版)
- Visual C++開發(fā)寶典
- Xamarin Cross-Platform Development Cookbook
- Building a Media Center with Raspberry Pi
- Raspberry Pi開發(fā)實(shí)戰(zhàn)