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

React你應(yīng)該學(xué)會(huì)的開(kāi)發(fā)技巧

開(kāi)發(fā) 前端
干凈的代碼不僅僅是工作代碼。簡(jiǎn)潔的代碼易于閱讀,易于理解并且井井有條。在本文中,我們將研究六種編寫更簡(jiǎn)潔的React代碼的方法。

[[385445]]

干凈的代碼不僅僅是工作代碼。簡(jiǎn)潔的代碼易于閱讀,易于理解并且井井有條。在本文中,我們將研究六種編寫更簡(jiǎn)潔的React代碼的方法。

在閱讀這些建議時(shí),請(qǐng)務(wù)必記住它們的實(shí)質(zhì):相信這些實(shí)踐對(duì)我們編寫自己的React代碼很有幫助。讓我們一起學(xué)習(xí)吧!

1.僅針對(duì)一種條件渲染

如果你要為某個(gè)條件成立時(shí)渲染某些元素,請(qǐng)不要使用三元運(yùn)算符。請(qǐng)改用&&運(yùn)算符。

不推薦寫法:

  1. import React, { useState } from 'react' 
  2. export const ConditionalRenderingWhenTrueBad = () => { 
  3.   const [showConditionalText, setShowConditionalText] = useState(false
  4.   const handleClick = () => 
  5.     setShowConditionalText(showConditionalText => !showConditionalText) 
  6.      
  7.   return ( 
  8.     <div> 
  9.       <button onClick={handleClick}>切換文本</button> 
  10.       {showConditionalText ? <p>成立顯示內(nèi)容</p> : null
  11.     </div> 
  12.   ) 

推薦寫法:

  1. import React, { useState } from 'react' 
  2. export const ConditionalRenderingWhenTrueGood = () => { 
  3.   const [showConditionalText, setShowConditionalText] = useState(false
  4.  
  5.   const handleClick = () => 
  6.     setShowConditionalText(showConditionalText => !showConditionalText) 
  7.  
  8.   return ( 
  9.     <div> 
  10.       <button onClick={handleClick}>切換文本</button> 
  11.       {showConditionalText && <p>成立顯示內(nèi)容!</p>} 
  12.     </div> 
  13.   ) 

2.Boolean Props簡(jiǎn)寫

isHungry處簡(jiǎn)寫了

不推薦寫法:

  1. import React from 'react' 
  2. const HungryMessage = ({ isHungry }) => ( 
  3.   <span>{isHungry ? 'I am hungry' : 'I am full'}</span> 
  4.  
  5. export const BooleanPropBad = () => ( 
  6.   <div> 
  7.     <HungryMessage isHungry={true} /> 
  8.     <HungryMessage isHungry={false} /> 
  9.   </div> 

推薦寫法:

  1. import React from 'react' 
  2. const HungryMessage = ({ isHungry }) => ( 
  3.   <span>{isHungry ? 'I am hungry' : 'I am full'}</span> 
  4.  
  5. export const BooleanPropGood = () => ( 
  6.   <div> 
  7.     <HungryMessage isHungry /> 
  8.     <HungryMessage isHungry={false} /> 
  9.   </div> 

3.String Props簡(jiǎn)寫

personName處簡(jiǎn)寫了

不推薦寫法:

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

推薦寫法:

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

4.事件處理函數(shù)簡(jiǎn)寫

onChange處簡(jiǎn)寫了

不推薦寫法:

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

推薦寫法:

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

5.組件作為參數(shù)返回

IconComponent處簡(jiǎn)寫了

不推薦寫法:

  1. import React from 'react' 
  2. const CircleIcon = () => ( 
  3.   <svg height="100" width="100"
  4.     <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> 
  5.   </svg> 
  6.  
  7. const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( 
  8.   <div> 
  9.     <IconComponent /> 
  10.   </div> 
  11.  
  12. export const UnnecessaryAnonymousFunctionComponentsBad = () => ( 
  13.   <ComponentThatAcceptsAnIcon IconComponent={() => <CircleIcon />} /> 

推薦寫法:

  1. import React from 'react' 
  2. const CircleIcon = () => ( 
  3.   <svg height="100" width="100"
  4.     <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> 
  5.   </svg> 
  6.  
  7. const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( 
  8.   <div> 
  9.     <IconComponent /> 
  10.   </div> 
  11.  
  12. export const UnnecessaryAnonymousFunctionComponentsGood = () => ( 
  13.   <ComponentThatAcceptsAnIcon IconComponent={CircleIcon} /> 

6.設(shè)置依賴于先前pros的pros

如果新?tīng)顟B(tài)依賴于先前狀態(tài),則始終將狀態(tài)設(shè)置為先前狀態(tài)的函數(shù)??梢耘幚鞷eact狀態(tài)更新,并且不以這種方式編寫更新會(huì)導(dǎo)致意外結(jié)果,setIsDisabled處簡(jiǎn)寫

不推薦寫法:

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

推薦寫法:

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

本文轉(zhuǎn)載自微信公眾號(hào)「前端人」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系前端人公眾號(hào)。

 

責(zé)任編輯:武曉燕 來(lái)源: 前端人
相關(guān)推薦

2023-01-03 09:00:52

React前端

2021-10-09 10:50:30

JavaScript編程開(kāi)發(fā)

2011-03-25 15:56:58

2013-01-09 13:55:43

2022-07-18 08:08:16

Go?語(yǔ)言技巧

2020-02-21 10:30:10

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

2020-06-02 10:10:46

React前端組件

2021-10-25 14:55:38

Linux技巧命令

2022-10-24 00:44:32

IO遠(yuǎn)程操作數(shù)據(jù)庫(kù)

2022-06-29 10:06:27

Webpack優(yōu)化技巧前端

2014-03-04 09:35:45

JavaScript調(diào)試

2022-11-16 09:04:36

SQL查詢SELECT

2023-12-07 07:03:09

2020-04-03 19:21:59

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

2021-04-12 15:54:45

Android 開(kāi)發(fā)技巧

2023-08-22 10:25:19

CSS動(dòng)畫網(wǎng)頁(yè)

2015-05-07 10:23:19

Android學(xué)習(xí)資源

2021-06-26 10:04:23

Code特性技巧

2010-11-09 10:03:26

2024-01-30 08:30:41

TypeScript編譯器類型
點(diǎn)贊
收藏

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