新聞中心
前言
干凈的代碼易于閱讀,簡單易懂,而且組織整齊。在這篇文章中,列舉了一些平時可能需要關(guān)注的點。

成都創(chuàng)新互聯(lián)公司網(wǎng)站設(shè)計,為客戶量身定制各類網(wǎng)站建設(shè)業(yè)務(wù),包括企業(yè)型、電子商務(wù)型、響應(yīng)式網(wǎng)站、行業(yè)門戶型等各類網(wǎng)站,實戰(zhàn)經(jīng)驗豐富,成功案例眾多。以客戶利益為出發(fā)點,成都創(chuàng)新互聯(lián)公司網(wǎng)站制作為客戶規(guī)劃、定制制作符合企業(yè)需求、帶有營銷價值的網(wǎng)絡(luò)建站方案認真對待每一個客戶,我們不用口頭的語言來吹擂我們的優(yōu)秀,上1000+的成功案例見證著我們的成長。
如果你不同意其中任何一條,那也完全沒問題。
只對一個條件進行條件性渲染
如果你需要在一個條件為真時有條件地呈現(xiàn)一些東西,在一個條件為假時不呈現(xiàn)任何東西,不要使用三元運算符。使用&&運算符代替。
糟糕的例子:
- import React, { useState } from 'react'
- export const ConditionalRenderingWhenTrueBad = () => {
- const [showConditionalText, setShowConditionalText] = useState(false)
- const handleClick = () =>
- setShowConditionalText(showConditionalText => !showConditionalText)
- return (
- {showConditionalText ?
The condition must be true!
: null}- )
- }
好的例子:
- import React, { useState } from 'react'
- export const ConditionalRenderingWhenTrueGood = () => {
- const [showConditionalText, setShowConditionalText] = useState(false)
- const handleClick = () =>
- setShowConditionalText(showConditionalText => !showConditionalText)
- return (
- {showConditionalText &&
The condition must be true!
}- )
- }
有條件的渲染是指在任何條件下
如果你需要在一個條件為真時有條件地呈現(xiàn)一個東西,在條件為假時呈現(xiàn)另一個東西,請使用三元運算符。
糟糕的例子:
- import React, { useState } from 'react'
- export const ConditionalRenderingBad = () => {
- const [showConditionOneText, setShowConditionOneText] = useState(false)
- const handleClick = () =>
- setShowConditionOneText(showConditionOneText => !showConditionOneText)
- return (
- {showConditionOneText &&
The condition must be true!
}- {!showConditionOneText &&
The condition must be false!
}- )
- }
好的例子:
- 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!
- )}
- )
- }
Boolean props
一個真實的props可以提供給一個組件,只有props名稱而沒有值,比如:myTruthyProp。寫成myTruthyProp={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:
- This person is full:
- )
String props
可以用雙引號提供一個字符串道具值,而不使用大括號或反斜線。
糟糕的例子:
- import React from 'react'
- const Greeting = ({ personName }) =>
Hi, {personName}!
- export const StringPropValuesBad = () => (
- )
好的例子:
- import React from 'react'
- const Greeting = ({ personName }) =>
Hi, {personName}!
- export const StringPropValuesGood = () => (
- )
事件處理函數(shù)
如果一個事件處理程序只需要事件對象的一個參數(shù),你就可以像這樣提供函數(shù)作為事件處理程序:onChange={handleChange}。
你不需要像這樣把函數(shù)包在一個匿名函數(shù)中。
糟糕的例子:
- import React, { useState } from 'react'
- export const UnnecessaryAnonymousFunctionsBad = () => {
- const [inputValue, setInputValue] = useState('')
- const handleChange = e => {
- setInputValue(e.target.value)
- }
- return (
- <>
- handleChange(e)} />
- >
- )
- }
好的例子:
- import React, { useState } from 'react'
- export const UnnecessaryAnonymousFunctionsGood = () => {
- const [inputValue, setInputValue] = useState('')
- const handleChange = e => {
- setInputValue(e.target.value)
- }
- return (
- <>
- >
- )
- }
將組件作為props傳遞
當把一個組件作為props傳遞給另一個組件時,如果該組件不接受任何props,你就不需要把這個傳遞的組件包裹在一個函數(shù)中。
糟糕的例子:
- import React from 'react'
- const CircleIcon = () => (
- )
- const ComponentThatAcceptsAnIcon = ({ IconComponent }) => (
Below is the icon component prop I was given:
- )
- export const UnnecessaryAnonymousFunctionComponentsBad = () => (
} /> - )
好的例子:
- import React from 'react'
- const CircleIcon = () => (
- )
- const ComponentThatAcceptsAnIcon = ({ IconComponent }) => (
Below is the icon component prop I was given:
- )
- export const UnnecessaryAnonymousFunctionComponentsGood = () => (
- )
為定義的props
未定義的props被排除在外,所以如果props未定義是可以的,就不要擔心提供未定義的回退。
糟糕的例子:
- 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!')} /> - )
設(shè)置依賴前一個狀態(tài)的狀態(tài)
如果新的狀態(tài)依賴于之前的狀態(tài),那么一定要把狀態(tài)設(shè)置為之前狀態(tài)的函數(shù)。React的狀態(tài)更新可以是分批進行的,如果不這樣寫你的更新就會導致意外的結(jié)果。
糟糕的例子:
- 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)
- const toggleButton = () => setIsDisabled(isDisabled => !isDisabled)
- const toggleButton2Times = () => {
- for (let i = 0; i < 2; i++) {
- toggleButton()
- }
- }
- return (
- I'm {isDisabled ? 'disabled' : 'enabled'}
- )
- }
總結(jié)
以下做法并非針對React,而是在JavaScript(以及任何編程語言)中編寫干凈代碼的良好做法。
稍微做個總結(jié):
- 將復雜的邏輯提取為明確命名的函數(shù)
- 將神奇的數(shù)字提取為常量
- 使用明確命名的變量
我是TianTian,我們下一期見!!!
分享題目:編寫簡潔的React代碼建議
網(wǎng)站地址:http://m.5511xx.com/article/copoppc.html


咨詢
建站咨詢
