新聞中心
整潔的代碼不僅僅是正常運(yùn)行的代碼,更是要求易于閱讀、簡單易懂、組織整齊。

在本文中,我們將研究八種代碼整潔之道。
在閱讀這些建議時(shí),要記住這些只是建議!如果你不同意它們中的任何一個(gè),那也完全沒關(guān)系。
以下這些實(shí)踐,個(gè)人覺得對我自己編寫 React 代碼很有幫助。
讓我們開始吧!
1. 僅對一個(gè)條件進(jìn)行渲染
如果需要在條件為 true 時(shí)渲染某些內(nèi)容,而在條件為 false 時(shí)不渲染任何內(nèi)容,不要使 三元表達(dá)式,請改用 &&。
?? 不推薦示例:
- import React, { useState } from 'react'
- export const ConditionalRenderingWhenTrueBad = () => {
- const [showConditionalText, setShowConditionalText] = useState(false)
- const handleClick = () =>
- setShowConditionalText(showConditionalText => !showConditionalText)
- return (
- {/* 三元表達(dá)式 */}
- {showConditionalText ?
條件為 True!
: null}- )
- }
推薦示例:
- import React, { useState } from 'react'
- export const ConditionalRenderingWhenTrueGood = () => {
- const [showConditionalText, setShowConditionalText] = useState(false)
- const handleClick = () =>
- setShowConditionalText(showConditionalText => !showConditionalText)
- return (
- {showConditionalText &&
條件為 True!
}- )
- }
2. 每一個(gè)條件都進(jìn)行渲染
如果需要在條件為 true 時(shí)渲染某些內(nèi)容,而在條件為 false 時(shí)渲染其他內(nèi)容。使用三元表達(dá)式!
?? 不推薦的示例:
- import React, { useState } from 'react'
- export const ConditionalRenderingBad = () => {
- const [showConditionOneText, setShowConditionOneText] = useState(false)
- const handleClick = () =>
- setShowConditionOneText(showConditionOneText => !showConditionOneText)
- return (
- {/* 條件 True 和 False 都要渲染內(nèi)容 */}
- {showConditionOneText &&
條件為 True!
}- {!showConditionOneText &&
條件為 Flase!
}- )
- }
推薦示例:
- import React, { useState } from 'react'
- export const ConditionalRenderingGood = () => {
- const [showConditionOneText, setShowConditionOneText] = useState(false)
- const handleClick = () =>
- setShowConditionOneText(showConditionOneText => !showConditionOneText)
- return (
- {showConditionOneText ? (
The condition must be true!
- ) : (
The condition must be false!
- )}
- )
- }
3. Boolean props
Props 值為 true 的推薦省略不寫。
?? 不推薦示例:
- import React from 'react'
- const HungryMessage = ({ isHungry }) => (
- {isHungry ? 'I am hungry' : 'I am full'}
- )
- export const BooleanPropBad = () => (
- This person is hungry:
- This person is full:
- )
推薦示例:
- import React from 'react'
- const HungryMessage = ({ isHungry }) => (
- {isHungry ? 'I am hungry' : 'I am full'}
- )
- export const BooleanPropGood = () => (
- This person is hungry:
- {/* 不需要賦值 true,省略 */}
- This person is full:
- )
4. String props
Props 值為 String, 使用雙引號,不使用花括號或反引號。
?? 不推薦示例:
- import React from 'react'
- const Greeting = ({ personName }) =>
Hi, {personName}!
- export const StringPropValuesBad = () => (
- )
推薦示例:
- import React from 'react'
- const Greeting = ({ personName }) =>
Hi, {personName}!
- export const StringPropValuesGood = () => (
- )
5. Event handler functions
如果一個(gè)事件函數(shù)只接受一個(gè)參數(shù),不需要傳入匿名函數(shù):onChange={e=>handleChange(e)},推薦這種寫法:onChange={handleChange} 。
?? 不推薦示例:
- import React, { useState } from 'react'
- export const UnnecessaryAnonymousFunctionsBad = () => {
- const [inputValue, setInputValue] = useState('')
- const handleChange = e => {
- setInputValue(e.target.value)
- }
- return (
- <>
- {/* 事件只有一個(gè)參數(shù),不需要匿名函數(shù)*/}
- handleChange(e)} />
- >
- )
- }
推薦示例:
- import React, { useState } from 'react'
- export const UnnecessaryAnonymousFunctionsGood = () => {
- const [inputValue, setInputValue] = useState('')
- const handleChange = e => {
- setInputValue(e.target.value)
- }
- return (
- <>
- >
- )
- }
6. components as props
將組件作為參數(shù)傳遞給另一個(gè)組件時(shí),如果該組件不接受任何參數(shù),則無需將該傳遞的組件包裝在函數(shù)中。
?? 不推薦示例:
- import React from 'react'
- const CircleIcon = () => (
- )
- const ComponentThatAcceptsAnIcon = ({ IconComponent }) => (
Below is the icon component prop I was given:
- )
- export const UnnecessaryAnonymousFunctionComponentsBad = () => (
- {/* 組件不需要包裝在函數(shù)中 */}
} /> - )
推薦示例:
- import React from 'react'
- const CircleIcon = () => (
- )
- const ComponentThatAcceptsAnIcon = ({ IconComponent }) => (
Below is the icon component prop I was given:
- )
- export const UnnecessaryAnonymousFunctionComponentsGood = () => (
- )
7. undefined props
如果參數(shù)為 undefined 是允許的,那么不要提供 undefined 作為回退值。
?? 不推薦示例:
- import React from 'react'
- const ButtonOne = ({ handleClick }) => (
- )
- const ButtonTwo = ({ handleClick }) => {
- const noop = () => {}
- return
- }
- export const UndefinedPropsBad = () => (
alert('Clicked!')} /> alert('Clicked!')} /> - )
推薦示例:
- import React from 'react'
- const ButtonOne = ({ handleClick }) => (
- )
- export const UndefinedPropsGood = () => (
alert('Clicked!')} /> - )
8. 設(shè)置 state 依賴先前的 state
如果新 state 依賴于先前 state,則始終將 state 設(shè)置為先前 state 的函數(shù)??梢耘幚?React 狀態(tài)更新。
?? 不推薦示例:
- import React, { useState } from 'react'
- export const PreviousStateBad = () => {
- const [isDisabled, setIsDisabled] = useState(false)
- const toggleButton = () => setIsDisabled(!isDisabled)
- const toggleButton2Times = () => {
- for (let i = 0; i < 2; i++) {
- toggleButton()
- }
- }
- return (
- I'm {isDisabled ? 'disabled' : 'enabled'}
- )
- }
推薦示例:
- import React, { useState } from 'react'
- export const PreviousStateGood = () => {
- const [isDisabled, setIsDisabled] = useState(false)
- {/* 推薦設(shè)置為函數(shù) */}
- const toggleButton = () => setIsDisabled(isDisabled => !isDisabled)
- const toggleButton2Times = () => {
- for (let i = 0; i < 2; i++) {
- toggleButton()
- }
- }
- return (
- I'm {isDisabled ? 'disabled' : 'enabled'}
- )
- }
以上就是我推薦的幾個(gè)寫出整潔的 React 代碼的實(shí)踐。
最后,恭喜你讀完了本文,歡迎留言交流~
原文地址:https://dev.to/thawkin3/react-clean-code-simple-ways-to-write-better-and-cleaner-code-2loa
翻譯/潤色:ViktorHub
名稱欄目:【譯】React代碼整潔之道
轉(zhuǎn)載來于:http://m.5511xx.com/article/cooeccp.html


咨詢
建站咨詢
