日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
編寫簡潔的React代碼建議

前言

干凈的代碼易于閱讀,簡單易懂,而且組織整齊。在這篇文章中,列舉了一些平時可能需要關(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)任何東西,不要使用三元運算符。使用&&運算符代替。

糟糕的例子:

 
 
 
  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.     
     
  11.       Toggle the text 
  12.       {showConditionalText ? 

    The condition must be true!

     : null} 
  13.     
 
  •   ) 
  •  好的例子:

     
     
     
    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.     
       
    11.       Toggle the text 
    12.       {showConditionalText && 

      The condition must be true!

    13.     
     
  •   ) 
  •  有條件的渲染是指在任何條件下

    如果你需要在一個條件為真時有條件地呈現(xiàn)一個東西,在條件為假時呈現(xià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.     
       
    11.       Toggle the text 
    12.       {showConditionOneText && 

      The condition must be true!

    13.       {!showConditionOneText && 

      The condition must be false!

    14.     
     
  •   ) 
  •  好的例子:

     
     
     
    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.     
       
    11.       Toggle the text 
    12.       {showConditionOneText ? ( 
    13.         

      The condition must be true!

       
    14.       ) : ( 
    15.         

      The condition must be false!

       
    16.       )} 
    17.     
     
  •   ) 
  •  Boolean props

    一個真實的props可以提供給一個組件,只有props名稱而沒有值,比如:myTruthyProp。寫成myTruthyProp={true}是不必要的。

    糟糕的例子:

     
     
     
    1. import React from 'react' 
    2.  
    3. const HungryMessage = ({ isHungry }) => ( 
    4.   {isHungry ? 'I am hungry' : 'I am full'} 
    5.  
    6. export const BooleanPropBad = () => ( 
    7.   
       
    8.      
    9.       This person is hungry:  
    10.      
    11.      
    12.      
    13.      
    14.       This person is full:  
    15.      
    16.      
    17.   
     
  •  好的例子:

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

    可以用雙引號提供一個字符串道具值,而不使用大括號或反斜線。

    糟糕的例子:

     
     
     
    1. import React from 'react' 
    2.  
    3. const Greeting = ({ personName }) => 

      Hi, {personName}!

       
    4.  
    5. export const StringPropValuesBad = () => ( 
    6.   
       
    7.      
    8.      
    9.      
    10.   
     
  •  好的例子:

     
     
     
    1. import React from 'react' 
    2.  
    3. const Greeting = ({ personName }) => 

      Hi, {personName}!

       
    4.  
    5. export const StringPropValuesGood = () => ( 
    6.   
       
    7.      
    8.      
    9.      
    10.   
     
  •  事件處理函數(shù)

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

    你不需要像這樣把函數(shù)包在一個匿名函數(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.       Name:  
    13.        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.       Name:  
    13.        
    14.      
    15.   ) 

    將組件作為props傳遞

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

    糟糕的例子:

     
     
     
    1. import React from 'react' 
    2.  
    3. const CircleIcon = () => ( 
    4.    
    5.      
    6.    
    7.  
    8. const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( 
    9.   
       
    10.     

      Below is the icon component prop I was given:

       
    11.      
    12.   
     
  •  
  • export const UnnecessaryAnonymousFunctionComponentsBad = () => ( 
  •    } /> 
  • 好的例子:

     
     
     
    1. import React from 'react' 
    2.  
    3. const CircleIcon = () => ( 
    4.    
    5.      
    6.    
    7.  
    8. const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( 
    9.   
       
    10.     

      Below is the icon component prop I was given:

       
    11.      
    12.   
     
  •  
  • export const UnnecessaryAnonymousFunctionComponentsGood = () => ( 
  •    
  • 為定義的props

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

    糟糕的例子:

     
     
     
    1. import React from 'react' 
    2.  
    3. const ButtonOne = ({ handleClick }) => ( 
    4.   Click me 
    5.  
    6. const ButtonTwo = ({ handleClick }) => { 
    7.   const noop = () => {} 
    8.  
    9.   return Click me 
    10.  
    11. export const UndefinedPropsBad = () => ( 
    12.   
       
    13.      
    14.      alert('Clicked!')} /> 
    15.      
    16.      alert('Clicked!')} /> 
    17.   
     
  •  好的例子:

     
     
     
    1. import React from 'react' 
    2.  
    3. const ButtonOne = ({ handleClick }) => ( 
    4.   Click me 
    5.  
    6. export const UndefinedPropsGood = () => ( 
    7.   
       
    8.      
    9.      alert('Clicked!')} /> 
    10.   
     
  •  設(shè)置依賴前一個狀態(tài)的狀態(tài)

    如果新的狀態(tài)依賴于之前的狀態(tài),那么一定要把狀態(tài)設(shè)置為之前狀態(tài)的函數(shù)。React的狀態(tài)更新可以是分批進行的,如果不這樣寫你的更新就會導致意外的結(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.     
       
    16.        
    17.         I'm {isDisabled ? 'disabled' : 'enabled'} 
    18.        
    19.       Toggle button state 
    20.       Toggle button state 2 times 
    21.      
    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.     
       
    16.        
    17.         I'm {isDisabled ? 'disabled' : 'enabled'} 
    18.        
    19.       Toggle button state 
    20.       Toggle button state 2 times 
    21.      
    22.   ) 

     總結(jié)

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

    稍微做個總結(jié):

    我是TianTian,我們下一期見!!!


    分享題目:編寫簡潔的React代碼建議
    網(wǎng)站地址:http://m.5511xx.com/article/copoppc.html

    其他資訊