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

成都創(chuàng)新互聯(lián)公司網(wǎng)站設(shè)計(jì),為客戶量身定制各類網(wǎng)站建設(shè)業(yè)務(wù),包括企業(yè)型、電子商務(wù)型、響應(yīng)式網(wǎng)站、行業(yè)門戶型等各類網(wǎng)站,實(shí)戰(zhàn)經(jīng)驗(yàn)豐富,成功案例眾多。以客戶利益為出發(fā)點(diǎn),成都創(chuàng)新互聯(lián)公司網(wǎng)站制作為客戶規(guī)劃、定制制作符合企業(yè)需求、帶有營銷價(jià)值的網(wǎng)絡(luò)建站方案認(rèn)真對(duì)待每一個(gè)客戶,我們不用口頭的語言來吹擂我們的優(yōu)秀,上1000+的成功案例見證著我們的成長(zhǎng)。
如果你不同意其中任何一條,那也完全沒問題。
只對(duì)一個(gè)條件進(jìn)行條件性渲染
如果你需要在一個(gè)條件為真時(shí)有條件地呈現(xiàn)一些東西,在一個(gè)條件為假時(shí)不呈現(xiàn)任何東西,不要使用三元運(yùn)算符。使用&&運(yù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!
}- )
- }
有條件的渲染是指在任何條件下
如果你需要在一個(gè)條件為真時(shí)有條件地呈現(xiàn)一個(gè)東西,在條件為假時(shí)呈現(xiàn)另一個(gè)東西,請(qǐng)使用三元運(yù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
一個(gè)真實(shí)的props可以提供給一個(gè)組件,只有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
可以用雙引號(hào)提供一個(gè)字符串道具值,而不使用大括號(hào)或反斜線。
糟糕的例子:
- 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ù)
如果一個(gè)事件處理程序只需要事件對(duì)象的一個(gè)參數(shù),你就可以像這樣提供函數(shù)作為事件處理程序:onChange={handleChange}。
你不需要像這樣把函數(shù)包在一個(gè)匿名函數(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傳遞
當(dāng)把一個(gè)組件作為props傳遞給另一個(gè)組件時(shí),如果該組件不接受任何props,你就不需要把這個(gè)傳遞的組件包裹在一個(gè)函數(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未定義是可以的,就不要擔(dān)心提供未定義的回退。
糟糕的例子:
- 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è)置依賴前一個(gè)狀態(tài)的狀態(tài)
如果新的狀態(tài)依賴于之前的狀態(tài),那么一定要把狀態(tài)設(shè)置為之前狀態(tài)的函數(shù)。React的狀態(tài)更新可以是分批進(jìn)行的,如果不這樣寫你的更新就會(huì)導(dǎo)致意外的結(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é)
以下做法并非針對(duì)React,而是在JavaScript(以及任何編程語言)中編寫干凈代碼的良好做法。
稍微做個(gè)總結(jié):
- 將復(fù)雜的邏輯提取為明確命名的函數(shù)
- 將神奇的數(shù)字提取為常量
- 使用明確命名的變量
我是TianTian,我們下一期見!!!
標(biāo)題名稱:編寫簡(jiǎn)潔的React代碼建議
網(wǎng)頁URL:http://www.5511xx.com/article/copoppc.html


咨詢
建站咨詢
