成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

React你應該學會的開發技巧

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

[[385445]]

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

在閱讀這些建議時,請務必記住它們的實質:相信這些實踐對我們編寫自己的React代碼很有幫助。讓我們一起學習吧!

1.僅針對一種條件渲染

如果你要為某個條件成立時渲染某些元素,請不要使用三元運算符。請改用&&運算符。

不推薦寫法:

  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>成立顯示內容</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>成立顯示內容!</p>} 
  12.     </div> 
  13.   ) 

2.Boolean Props簡寫

isHungry處簡寫了

不推薦寫法:

  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簡寫

personName處簡寫了

不推薦寫法:

  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.事件處理函數簡寫

onChange處簡寫了

不推薦寫法:

  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.組件作為參數返回

IconComponent處簡寫了

不推薦寫法:

  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.設置依賴于先前pros的pros

如果新狀態依賴于先前狀態,則始終將狀態設置為先前狀態的函數??梢耘幚鞷eact狀態更新,并且不以這種方式編寫更新會導致意外結果,setIsDisabled處簡寫

不推薦寫法:

  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}>切換按鈕狀態</button> 
  18.       <button onClick={toggleButton2Times}>切換按鈕狀態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}>切換按鈕狀態</button> 
  18.       <button onClick={toggleButton2Times}>切換按鈕狀態2次</button> 
  19.     </div> 
  20.   ) 

本文轉載自微信公眾號「前端人」,可以通過以下二維碼關注。轉載本文請聯系前端人公眾號。

 

責任編輯:武曉燕 來源: 前端人
相關推薦

2023-01-03 09:00:52

React前端

2021-10-09 10:50:30

JavaScript編程開發

2011-03-25 15:56:58

2013-01-09 13:55:43

2022-07-18 08:08:16

Go?語言技巧

2020-06-02 10:10:46

React前端組件

2020-02-21 10:30:10

開發技能代碼

2021-10-25 14:55:38

Linux技巧命令

2022-10-24 00:44:32

IO遠程操作數據庫

2022-06-29 10:06:27

Webpack優化技巧前端

2014-03-04 09:35:45

JavaScript調試

2022-11-16 09:04:36

SQL查詢SELECT

2023-12-07 07:03:09

2020-04-03 19:21:59

JavaScript編程語言開發

2021-04-12 15:54:45

Android 開發技巧

2015-05-07 10:23:19

Android學習資源

2010-11-09 10:03:26

2021-06-26 10:04:23

Code特性技巧

2023-08-22 10:25:19

CSS動畫網頁

2024-01-30 08:30:41

TypeScript編譯器類型
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91精品久久 | 久草资源在线视频 | 国产在线精品一区二区 | 国产伦精品一区二区三区高清 | 国产精品午夜电影 | 伊人一区 | av日韩精品 | 日本久久精品视频 | 91精品国产综合久久精品 | 日本中文字幕视频 | 伊人网站在线 | avtt国产| 日韩在线成人 | 欧美在线精品一区 | 韩国av一区二区 | 色性av| 免费视频一区二区三区在线观看 | 日韩欧美国产精品一区二区 | 精品在线 | 国产成人精品在线 | 精品国产伦一区二区三区观看体验 | 午夜99| 成人在线免费视频 | 久久av网| 老熟女毛片 | 久久久不卡网国产精品一区 | 久久久精品视频免费 | 91久久| 成人欧美一区二区三区在线播放 | 男女啪啪高潮无遮挡免费动态 | 亚洲综合色视频在线观看 | 国产精品久久片 | 曰批视频在线观看 | 午夜精品一区二区三区在线观看 | 成人在线视频免费看 | 久久不卡 | 欧美日韩精品中文字幕 | 久久精品一区 | 中文字幕国产第一页 | 国产精品乱码一二三区的特点 | 成人免费观看男女羞羞视频 |