当前位置: 首页 > 科技观察

编写干净的React代码的技巧

时间:2023-03-21 13:22:08 科技观察

前言干净的代码易于阅读、易于理解且组织整齐。在这篇文章中,列出了一些可能需要注意的点。如果您不同意其中任何一个,那完全没问题。仅针对一个条件的条件渲染如果您需要在一个条件为真时有条件地渲染某些东西,而在一个条件为假时什么都不渲染,请不要使用三元运算符。请改用&&运算符。错误示例:importReact,{useState}from'react'exportconstConditionalRenderingWhenTrueBad=()=>{const[showConditionalText,setShowConditionalText]=useState(false)consthandleClick=()=>setShowConditionalText(showConditionalText=>!showConditionalText)return(

Togglethetext{showConditionalText?

Theconditionmustbetrue!

:null}
)}很好的例子:importReact,{useState}from'react'exportconstConditionalRenderingWhenTrueGood=()=>{const[showConditionalText,setShowConditionalText]=useState(false)constandleClick=()=>setShowConditionalText(showConditionalText=>!showConditionalText)return(
Togglethetext{showConditionalText&&

Theconditionmustbetrue!

}
)}条件渲染是指在任何条件下,如果你需要有条件地在一个条件为真时渲染一件事,在条件为假时有条件地渲染另一件事,请使用三元运算符。糕点的例子:importReact,{useState}from'react'exportconstConditionalRenderingBad=()=>{const[showConditionOneText,setShowConditionOneText]=useState(false)constandleClick=()=>setShowConditionOneText(showConditionOneText=>!showConditionOneText)return(
Togglethetext{showConditionOneText&&

Theconditionmustbetrue!

}{??!showConditionOneText&&

Theconditionmustbefalse!

}
)}好的例子:importReact,{useState}from'react'exportconstconditionalRenderingGood=()=>{const[showConditionOneText,setShowConditionOneText]=useState(false)constandleClick=()=>setShowConditionOneText(showConditionOneText=>!showConditionOneText)返回(
Togglethetext{showConditionOneText?(

Theconditionmustbetrue!

):(

Theconditionmustbefalse!

)}
)}布尔属性一个真实的props可以提供给只有propsname而没有value的组件,例如:myTruthyProp写成myTruthyProp={true}是不必要的。错误示例:importReactfrom'react'constHungryMessage=({isHungry})=>({isHungry?'Iamhungry':'Iamfull'})exportconstBooleanPropBad=()=>(
Thispersonishungry:
Thispersonisfull:/div>)很好的例子:importReactfrom'react'constHungryMessage=({isHungry})=>({isHungry?'Iamhungry':'Iamfull'})exportconstBooleanPropGood=()=>(
>span>Thispersonishungry:
Thispersonisfull:
)Stringprops可以在双引号中提供字符串prop值,没有大括号或反斜杠。错误示例:importReactfrom'react'constGreeting=({personName})=>

嗨,{personName}!

exportconstStringPropValuesBad=()=>(
)很好的例子:importReactfrom'react'constGreeting=({personName})=>

嗨,{personName}!

exportconstStringPropValuesGood=()=>(
)事件处理程序如果事件处理程序只需要事件对象的一个??参数,您可以像这样提供函数作为事件处理程序:onChange={handleChange}。您不需要像这样将函数包装在匿名函数中。错误示例:importReact,{useState}from'react'exportconstUnnecessaryAnonymousFunctionsBad=()=>{const[inputValue,setInputValue]=useState('')constandleChange=e=>{setInputValue(e.target.value)}return(<>Name:handleChange(e)}/>)}很好的例子:importReact,{useState}from'react'exportconstUnnecessaryAnonymousFunctionsGood=()=>{const[inputValue,setInputValue]=useState('')constandleChange=e=>{setInputValue(e.target.value)}return(<>Name:>)}将组件作为props传递当将组件作为props传递给另一个组件时,如果组件不接受任何props,所以你不需要将这个传递的组件包装在一个函数中。糕点的例子:importReactfrom'react'constCircleIcon=()=>()constComponentThatAcceptsAnIcon=({IconComponent})=>(

BelowistheiconcomponentpropIwasgiven:

)exportconstUnnecessaryAnonymousFunctionComponentsBad=()=>(}/>)好的例子:importReactfrom'react'constCircleIcon=()=>()constComponentThatAcceptsAnIcon=({IconComponent})=>(

下面是iconcomponentpropIwasgiven:

)exportconstUnnecessaryAnonymousFunctionComponentsGood=()=>()对于已定义的props未定义的props被排除在外,所以如果propsundefined没问题,不用担心提供未定义的回退。错误示例:importReactfrom'react'constButtonOne=({handleClick})=>(Clickme)constButtonTwo=({handleClick})=>{constnoop=()=>{}返回Clickme}exportconstUndefinedPropsBad=()=>(
alert('Clicked!')}/>alert('Clicked!')}/>
)很好的例子:importReactfrom'react'constButtonOne=({handleClick})=>(Clickme)exportconstUndefinedPropsGood=()=>(
alert('Clicked!')}/>
)设置一个依赖于的状态thepreviousstateif新状态依赖于之前的状态,所以一定要将状态设置为之前状态的函数。React的状态更新可以批处理,否则编写更新可能会导致意想不到的结果。糕点的例子:importReact,{useState}from'react'exportconstPreviousStateBad=()=>{const[isDisabled,setIsDisabled]=useState(false)consttoggleButton=()=>setIsDisabled(!isDisabled)consttoggleButton2Times=()=>{for(leti=0;i<2;i++){toggleButton()}}return(
我{isDisabled?'disabled':'enabled'}TogglebuttonstateTogglebuttonstate2times
)}好的例子:importReact,{useState}from'react'exportconstPreviousStateGood=()=>{const[isDisabled,setIsDisabled]=useState(false)consttoggleButton=()=>setIsDisabled(isDisabled=>!isDisabled)consttoggleButton2Times=()=>{for(leti=0;i<2;i++){toggleButton()}}返回(
我{isDisabled?'disabled':'enabled'}TogglebuttonstateTogglebuttonstate2times
)}总结以下实践不是React特有的,而是用JavaScript(和任何编程语言)编写干净代码的良好实践一点总结:将复杂逻辑提取到显式命名函数中提取幻数作为常量使用明确命名的变量我是天天,下期见!!!