美文网首页
前端Coding规范 — React&JSX 书写规范

前端Coding规范 — React&JSX 书写规范

作者: Yong_bcf4 | 来源:发表于2022-06-06 09:00 被阅读0次

基本规则

  • 每个文件只包含一个 React 类组件
  • 一般使用 JSX 语法
  • 除非是在非 JSX 文件中初始化应用,否则不要使用 React.createElement

命名规范

  • 组件文件扩展名

如果使用 JavaScript,则文件扩展名为 .js;如果使用 TypeScript,则文件扩展名为 .tsx

  • 组件文件名

如果是组件文件,则使用 PascalCase,如 MyComponent.js

如果组件是一个目录,则组件主入口命名为 index,如 index.js

  • 引用命名

React 组件使用 PascalCase,组件实例使用 CamelCase,eslint: react/jsx-pascal-case

// bad
import reservationCard from './ReservationCard'

// good
import ReservationCard from './ReservationCard'

// bad
const ReservationItem = <ReservationCard />

// good
const reservationItem = <ReservationCard /></pre>
  • 组件命名

使用文件名作为组件名字,例如, ReservationCard.js 应该包含名为 ReservationCard 的引用,然而对于文件夹中的根组件, 使用 index.js 作为文件名,使用文件夹的名字作为组件的名字

// bad
import Footer from './Footer/Footer'

// bad
import Footer from './Footer/index'

// good
import Footer from './Footer'</pre>
  • 组件属性名

React DOM 使用小驼峰式命名法来定义属性的名称,而不使用 HTML 属性名称的命名约定,例如

<div onClick={this.handler} />

Class Component VS Functional Component

只允许使用 Class ComponentFunctional Component 两种形态来书写组件,建议尽量使用函数式组件配合 Hooks 来进行开发

对齐

遵循以下JSX语法的对齐风格,eslint: react/jsx-closing-bracket-location

// bad
<Foo superLongParam='bar'
 anotherSuperLongParam='baz' />

// good
<Foo
 superLongParam='bar'
 anotherSuperLongParam='baz'
/>

// if props fit in one line then keep it on the same line
<Foo bar='bar' />

// children get indented normally
<Foo
 superLongParam='bar'
 anotherSuperLongParam='baz'
>
 <Quux />
</Foo>

// bad
{showButton &&
 <Button />
}

// bad
{
 showButton &&
 <Button />
}

// good
{showButton && (
 <Button />
)}

// good
{showButton && <Button />}</pre>

空格

// bad
<Foo/>

// bad
<Foo
 />

// good
<Foo />
// bad
<Foo bar={ baz } />

// good
<Foo bar={baz} />

引号

JSX 属性要使用单引号,与其他普通 JS 保持一致

// bad
<Foo bar="bar" />

// good
<Foo bar='bar' />

// bad
<Foo style={{ left: "20px" }} />

// good
<Foo style={{ left: '20px' }} />

属性

  • 属性名使用 CamelCase
// bad
<Foo
 UserName='hello'
 phone_number={12345678}
/>

// good
<Foo
 userName='hello'
 phoneNumber={12345678}
/>
// bad
<Foo
 hidden={true}
/>

// good
<Foo
 hidden
/>

// good
<Foo hidden />

原因:不使用稳定的 ID 会对性能产生副作用并且组件状态会出问题,是一种反模式

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

// good
{todos.map(todo => (
 <Todo
 {...todo}
 key={todo.id}
 />
))}
  • 为所有的非必需属性定义使用 defaultProps 明确的默认值
// bad
function SFC ({ foo, bar, children }) {
 return <div>{foo}{bar}{children}</div>
}
SFC.propTypes = {
 foo: PropTypes.number.isRequired,
 bar: PropTypes.string,
 children: PropTypes.node
}

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


### Refs

避免使用字符串引用,请使用回调函数作为引用,eslint: [react/no-string-refs](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md)

| 

<pre style="margin: 0px; padding: 0px; font-family: ConfluenceInstalledFont, monospace;">// bad
<Foo
 ref='myRef'
/>

// good
<Foo
 ref={ref => { this.myRef = ref }}
/>
``

### 圆括号

当 JSX 标签超过一行时使用圆括号包裹, eslint: [react/wrap-multilines](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md)

| 

<pre style="margin: 0px; padding: 0px; font-family: ConfluenceInstalledFont, monospace;">
// bad
render () {
 return <MyComponent className='long body' foo='bar'>
 <MyChild />
 </MyComponent>
}

// good
render () {
 return (
 <MyComponent className='long body' foo='bar'>
 <MyChild />
 </MyComponent>
 )
}

// good, when single line
render () {
 const body = <div>hello</div>
 return <MyComponent>{body}</MyComponent>
}

标签

// bad
<Foo className='stuff'></Foo>

// good
<Foo className='stuff' />
// bad
<Foo
 bar='bar'
 baz='baz' />

// good
<Foo
 bar='bar'
 baz='baz'
/>

方法

  • 使用箭头函数包裹本地变量
function ItemList (props) {
 return (
 <ul>
 {props.items.map((item, index) => (
 <Item
 key={item.key}
 onClick={() => doSomethingWith(item.name, index)}
 />
 ))}
 </ul>
 )
}
  • 类组件的内部方法不要使用下划线前缀
// bad
class extends React.Component {
 _onClickSubmit () {
 // do stuff
 }

 // other stuff
}

// good
class extends React.Component {
 onClickSubmit () {
 // do stuff
 }

 // other stuff
}
// bad
render () {
 (<div />)
}

// good
render () {
 return (<div />)
}

Hooks 书写规范

  • Hooks 只能应用于函数式组件中

  • 只在 React 函数最顶层使用 Hooks

不要在循环,条件或嵌套函数中调用 Hook, 确保总是在你的 React 函数的最顶层调用他们

// bad
function a () {
 const [count, setCount] = useState(0)
 useEffect(function persistForm() {
 localStorage.setItem('formData', accountName)
 })
 const x = function () {}
 const [timer, setTimer] = useState(0)

 // main logic
}

// bad
function a () {
 const [count, setCount] = useState(0)
 useEffect(function persistForm() {
 localStorage.setItem('formData', accountName)
 })
 const [timer, setTimer] = useState(0)
 const x = function () {}
 // main logic
}

相关文章

  • 前端规范

    前端规范 规范说明 此为前端开发团队遵循和约定的代码书写规范,意在提高代码的规范性和可维护性。此规范为参考规范,统...

  • css命名整理

    文章整理了Web前端开发中的各种CSS规范,包括文件规范、注释规范、命名规范、书写规范、测试规范等。 一、文件规范...

  • css命名规范整理

    文章整理了Web前端开发中的各种CSS规范,包括文件规范、注释规范、命名规范、书写规范、测试规范等。 一、文件规范...

  • 前端书写规范

    ==注重结构、表现、行为分离== 1. html结构 2. css表现 3. JavaScript

  • iOS Coding Style Guide 代码规范

    iOS Coding Style Guide 代码规范 iOS Coding Style Guide 代码规范

  • 移动前端开发规范(一般规范)

    系列目录 移动前端开发规范(一般规范)移动前端开发规范(技术栈规范)移动前端开发规范(HTML规范)移动前端开发规...

  • WEB前端规范

    1. 规范说明 此为前端开发团队遵循和约定的代码书写规范,意在提高代码的规范性和可维护性。此规范为参考规范,不全是...

  • coding_guide

    1. Coding指南 本规范的目的是提高制作效率和项目质量,统一前端人员的编码规范。 1.1. 基本规则 1.1...

  • 前端基本开发规范

    一般规范 HTML书写规范 CSS书写规范 JavaScript书写规范 一般规范 文件资源命名 资源(图片、js...

  • 前端书写规范建议

    1.项目、目录及文件命名 1.1 全部采用小写方式, 以下划线分隔。 项目命名: 用项目对应的英文单词命名。例:m...

网友评论

      本文标题:前端Coding规范 — React&JSX 书写规范

      本文链接:https://www.haomeiwen.com/subject/krfqmrtx.html