自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

編寫(xiě)簡(jiǎn)潔的React代碼建議

開(kāi)發(fā) 前端
如果你需要在一個(gè)條件為真時(shí)有條件地呈現(xiàn)一些東西,在一個(gè)條件為假時(shí)不呈現(xiàn)任何東西,不要使用三元運(yùn)算符。使用&&運(yùn)算符代替。

[[395577]]

前言

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

如果你不同意其中任何一條,那也完全沒(méi)問(wèn)題。

只對(duì)一個(gè)條件進(jìn)行條件性渲染

如果你需要在一個(gè)條件為真時(shí)有條件地呈現(xiàn)一些東西,在一個(gè)條件為假時(shí)不呈現(xiàn)任何東西,不要使用三元運(yùn)算符。使用&&運(yùn)算符代替。

糟糕的例子:

  1. import React, { useState } from 'react' 
  2.  
  3. export const ConditionalRenderingWhenTrueBad = () => { 
  4.   const [showConditionalText, setShowConditionalText] = useState(false
  5.  
  6.   const handleClick = () => 
  7.     setShowConditionalText(showConditionalText => !showConditionalText) 
  8.  
  9.   return ( 
  10.     <div> 
  11.       <button onClick={handleClick}>Toggle the text</button> 
  12.       {showConditionalText ? <p>The condition must be true!</p> : null
  13.     </div> 
  14.   ) 

 好的例子:

  1. import React, { useState } from 'react' 
  2.  
  3. export const ConditionalRenderingWhenTrueGood = () => { 
  4.   const [showConditionalText, setShowConditionalText] = useState(false
  5.  
  6.   const handleClick = () => 
  7.     setShowConditionalText(showConditionalText => !showConditionalText) 
  8.  
  9.   return ( 
  10.     <div> 
  11.       <button onClick={handleClick}>Toggle the text</button> 
  12.       {showConditionalText && <p>The condition must be true!</p>} 
  13.     </div> 
  14.   ) 

 有條件的渲染是指在任何條件下

如果你需要在一個(gè)條件為真時(shí)有條件地呈現(xiàn)一個(gè)東西,在條件為假時(shí)呈現(xiàn)另一個(gè)東西,請(qǐng)使用三元運(yùn)算符。

糟糕的例子:

  1. import React, { useState } from 'react' 
  2.  
  3. export const ConditionalRenderingBad = () => { 
  4.   const [showConditionOneText, setShowConditionOneText] = useState(false
  5.  
  6.   const handleClick = () => 
  7.     setShowConditionOneText(showConditionOneText => !showConditionOneText) 
  8.  
  9.   return ( 
  10.     <div> 
  11.       <button onClick={handleClick}>Toggle the text</button> 
  12.       {showConditionOneText && <p>The condition must be true!</p>} 
  13.       {!showConditionOneText && <p>The condition must be false!</p>} 
  14.     </div> 
  15.   ) 

 好的例子:

  1. import React, { useState } from 'react' 
  2.  
  3. export const ConditionalRenderingGood = () => { 
  4.   const [showConditionOneText, setShowConditionOneText] = useState(false
  5.  
  6.   const handleClick = () => 
  7.     setShowConditionOneText(showConditionOneText => !showConditionOneText) 
  8.  
  9.   return ( 
  10.     <div> 
  11.       <button onClick={handleClick}>Toggle the text</button> 
  12.       {showConditionOneText ? ( 
  13.         <p>The condition must be true!</p> 
  14.       ) : ( 
  15.         <p>The condition must be false!</p> 
  16.       )} 
  17.     </div> 
  18.   ) 

 Boolean props

一個(gè)真實(shí)的props可以提供給一個(gè)組件,只有props名稱(chēng)而沒(méi)有值,比如:myTruthyProp。寫(xiě)成myTruthyProp={true}是不必要的。

糟糕的例子:

  1. import React from 'react' 
  2.  
  3. const HungryMessage = ({ isHungry }) => ( 
  4.   <span>{isHungry ? 'I am hungry' : 'I am full'}</span> 
  5.  
  6. export const BooleanPropBad = () => ( 
  7.   <div> 
  8.     <span> 
  9.       <b>This person is hungry: </b> 
  10.     </span> 
  11.     <HungryMessage isHungry={true} /> 
  12.     <br /> 
  13.     <span> 
  14.       <b>This person is full: </b> 
  15.     </span> 
  16.     <HungryMessage isHungry={false} /> 
  17.   </div> 

 好的例子:

  1. import React from 'react' 
  2.  
  3. const HungryMessage = ({ isHungry }) => ( 
  4.   <span>{isHungry ? 'I am hungry' : 'I am full'}</span> 
  5.  
  6. export const BooleanPropGood = () => ( 
  7.   <div> 
  8.     <span> 
  9.       <b>This person is hungry: </b> 
  10.     </span> 
  11.     <HungryMessage isHungry /> 
  12.     <br /> 
  13.     <span> 
  14.       <b>This person is full: </b> 
  15.     </span> 
  16.     <HungryMessage isHungry={false} /> 
  17.   </div> 

 String props

可以用雙引號(hào)提供一個(gè)字符串道具值,而不使用大括號(hào)或反斜線。

糟糕的例子:

  1. import React from 'react' 
  2.  
  3. const Greeting = ({ personName }) => <p>Hi, {personName}!</p> 
  4.  
  5. export const StringPropValuesBad = () => ( 
  6.   <div> 
  7.     <Greeting personName={"John"} /> 
  8.     <Greeting personName={'Matt'} /> 
  9.     <Greeting personName={`Paul`} /> 
  10.   </div> 

 好的例子:

  1. import React from 'react' 
  2.  
  3. const Greeting = ({ personName }) => <p>Hi, {personName}!</p> 
  4.  
  5. export const StringPropValuesGood = () => ( 
  6.   <div> 
  7.     <Greeting personName="John" /> 
  8.     <Greeting personName="Matt" /> 
  9.     <Greeting personName="Paul" /> 
  10.   </div> 

 事件處理函數(shù)

如果一個(gè)事件處理程序只需要事件對(duì)象的一個(gè)參數(shù),你就可以像這樣提供函數(shù)作為事件處理程序:onChange={handleChange}。

你不需要像這樣把函數(shù)包在一個(gè)匿名函數(shù)中。

糟糕的例子:

  1. import React, { useState } from 'react' 
  2.  
  3. export const UnnecessaryAnonymousFunctionsBad = () => { 
  4.   const [inputValue, setInputValue] = useState(''
  5.  
  6.   const handleChange = e => { 
  7.     setInputValue(e.target.value) 
  8.   } 
  9.  
  10.   return ( 
  11.     <> 
  12.       <label htmlFor="name">Name: </label> 
  13.       <input id="name" value={inputValue} onChange={e => handleChange(e)} /> 
  14.     </> 
  15.   ) 

好的例子:

  1. import React, { useState } from 'react' 
  2.  
  3. export const UnnecessaryAnonymousFunctionsGood = () => { 
  4.   const [inputValue, setInputValue] = useState(''
  5.  
  6.   const handleChange = e => { 
  7.     setInputValue(e.target.value) 
  8.   } 
  9.  
  10.   return ( 
  11.     <> 
  12.       <label htmlFor="name">Name: </label> 
  13.       <input id="name" value={inputValue} onChange={handleChange} /> 
  14.     </> 
  15.   ) 

將組件作為props傳遞

當(dāng)把一個(gè)組件作為props傳遞給另一個(gè)組件時(shí),如果該組件不接受任何props,你就不需要把這個(gè)傳遞的組件包裹在一個(gè)函數(shù)中。

糟糕的例子:

  1. import React from 'react' 
  2.  
  3. const CircleIcon = () => ( 
  4.   <svg height="100" width="100"
  5.     <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> 
  6.   </svg> 
  7.  
  8. const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( 
  9.   <div> 
  10.     <p>Below is the icon component prop I was given:</p> 
  11.     <IconComponent /> 
  12.   </div> 
  13.  
  14. export const UnnecessaryAnonymousFunctionComponentsBad = () => ( 
  15.   <ComponentThatAcceptsAnIcon IconComponent={() => <CircleIcon />} /> 

好的例子:

  1. import React from 'react' 
  2.  
  3. const CircleIcon = () => ( 
  4.   <svg height="100" width="100"
  5.     <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> 
  6.   </svg> 
  7.  
  8. const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( 
  9.   <div> 
  10.     <p>Below is the icon component prop I was given:</p> 
  11.     <IconComponent /> 
  12.   </div> 
  13.  
  14. export const UnnecessaryAnonymousFunctionComponentsGood = () => ( 
  15.   <ComponentThatAcceptsAnIcon IconComponent={CircleIcon} /> 

為定義的props

未定義的props被排除在外,所以如果props未定義是可以的,就不要擔(dān)心提供未定義的回退。

糟糕的例子:

  1. import React from 'react' 
  2.  
  3. const ButtonOne = ({ handleClick }) => ( 
  4.   <button onClick={handleClick || undefined}>Click me</button> 
  5.  
  6. const ButtonTwo = ({ handleClick }) => { 
  7.   const noop = () => {} 
  8.  
  9.   return <button onClick={handleClick || noop}>Click me</button> 
  10.  
  11. export const UndefinedPropsBad = () => ( 
  12.   <div> 
  13.     <ButtonOne /> 
  14.     <ButtonOne handleClick={() => alert('Clicked!')} /> 
  15.     <ButtonTwo /> 
  16.     <ButtonTwo handleClick={() => alert('Clicked!')} /> 
  17.   </div> 

 好的例子:

  1. import React from 'react' 
  2.  
  3. const ButtonOne = ({ handleClick }) => ( 
  4.   <button onClick={handleClick}>Click me</button> 
  5.  
  6. export const UndefinedPropsGood = () => ( 
  7.   <div> 
  8.     <ButtonOne /> 
  9.     <ButtonOne handleClick={() => alert('Clicked!')} /> 
  10.   </div> 

 設(shè)置依賴(lài)前一個(gè)狀態(tài)的狀態(tài)

如果新的狀態(tài)依賴(lài)于之前的狀態(tài),那么一定要把狀態(tài)設(shè)置為之前狀態(tài)的函數(shù)。React的狀態(tài)更新可以是分批進(jìn)行的,如果不這樣寫(xiě)你的更新就會(huì)導(dǎo)致意外的結(jié)果。

糟糕的例子:

  1. import React, { useState } from 'react' 
  2.  
  3. export const PreviousStateBad = () => { 
  4.   const [isDisabled, setIsDisabled] = useState(false
  5.  
  6.   const toggleButton = () => setIsDisabled(!isDisabled) 
  7.  
  8.   const toggleButton2Times = () => { 
  9.     for (let i = 0; i < 2; i++) { 
  10.       toggleButton() 
  11.     } 
  12.   } 
  13.  
  14.   return ( 
  15.     <div> 
  16.       <button disabled={isDisabled}> 
  17.         I'm {isDisabled ? 'disabled' : 'enabled'} 
  18.       </button> 
  19.       <button onClick={toggleButton}>Toggle button state</button> 
  20.       <button onClick={toggleButton2Times}>Toggle button state 2 times</button> 
  21.     </div> 
  22.   ) 

 好的例子:

  1. import React, { useState } from 'react' 
  2.  
  3. export const PreviousStateGood = () => { 
  4.   const [isDisabled, setIsDisabled] = useState(false
  5.  
  6.   const toggleButton = () => setIsDisabled(isDisabled => !isDisabled) 
  7.  
  8.   const toggleButton2Times = () => { 
  9.     for (let i = 0; i < 2; i++) { 
  10.       toggleButton() 
  11.     } 
  12.   } 
  13.  
  14.   return ( 
  15.     <div> 
  16.       <button disabled={isDisabled}> 
  17.         I'm {isDisabled ? 'disabled' : 'enabled'} 
  18.       </button> 
  19.       <button onClick={toggleButton}>Toggle button state</button> 
  20.       <button onClick={toggleButton2Times}>Toggle button state 2 times</button> 
  21.     </div> 
  22.   ) 

 總結(jié)

以下做法并非針對(duì)React,而是在JavaScript(以及任何編程語(yǔ)言)中編寫(xiě)干凈代碼的良好做法。

稍微做個(gè)總結(jié):

  • 將復(fù)雜的邏輯提取為明確命名的函數(shù)
  • 將神奇的數(shù)字提取為常量
  • 使用明確命名的變量

我是TianTian,我們下一期見(jiàn)!!!

 

責(zé)任編輯:姜華 來(lái)源: TianTianUp
相關(guān)推薦

2022-06-27 06:23:23

代碼編程

2022-12-15 10:52:26

代碼開(kāi)發(fā)

2024-01-30 08:54:05

JavaScript技巧代碼

2012-04-27 16:54:57

Java代碼

2011-11-25 10:35:20

Java

2021-06-08 09:35:11

Cleaner ReaReact開(kāi)發(fā)React代碼

2020-08-06 16:34:48

Python開(kāi)發(fā)工具

2023-09-22 12:04:53

Java代碼

2022-08-28 19:03:18

JavaScript編程語(yǔ)言開(kāi)發(fā)

2017-08-28 14:58:19

CSSFlexbox注釋格式優(yōu)化

2016-09-07 19:58:47

CSS代碼Web

2020-10-04 13:15:37

代碼技術(shù)開(kāi)發(fā)

2022-05-10 10:28:21

JavaScript代碼

2024-06-03 11:43:55

2020-09-21 06:58:56

TS 代碼建議

2020-05-08 19:52:31

Reactreact.js前端

2017-10-10 16:28:51

前端CSS建議

2024-06-03 11:36:06

Pythonf-string

2017-02-28 21:57:05

React組件

2022-02-25 08:00:00

編程ReactTypescript
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)