這篇文章主要講解了“React應(yīng)該學(xué)會(huì)的開發(fā)技巧有哪些”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“React應(yīng)該學(xué)會(huì)的開發(fā)技巧有哪些”吧!
成都創(chuàng)新互聯(lián)公司專注于原陽網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供原陽營(yíng)銷型網(wǎng)站建設(shè),原陽網(wǎng)站制作、原陽網(wǎng)頁(yè)設(shè)計(jì)、原陽網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造原陽網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供原陽網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
1.僅針對(duì)一種條件渲染
如果你要為某個(gè)條件成立時(shí)渲染某些元素,請(qǐng)不要使用三元運(yùn)算符。請(qǐng)改用&&運(yùn)算符。
不推薦寫法:
import React, { useState } from 'react' export const ConditionalRenderingWhenTrueBad = () => { const [showConditionalText, setShowConditionalText] = useState(false) const handleClick = () => setShowConditionalText(showConditionalText => !showConditionalText) return ( <div> <button onClick={handleClick}>切換文本</button> {showConditionalText ? <p>成立顯示內(nèi)容</p> : null} </div> ) }
推薦寫法:
import React, { useState } from 'react' export const ConditionalRenderingWhenTrueGood = () => { const [showConditionalText, setShowConditionalText] = useState(false) const handleClick = () => setShowConditionalText(showConditionalText => !showConditionalText) return ( <div> <button onClick={handleClick}>切換文本</button> {showConditionalText && <p>成立顯示內(nèi)容!</p>} </div> ) }
2.Boolean Props簡(jiǎn)寫
isHungry處簡(jiǎn)寫了
不推薦寫法:
import React from 'react' const HungryMessage = ({ isHungry }) => ( <span>{isHungry ? 'I am hungry' : 'I am full'}</span> ) export const BooleanPropBad = () => ( <div> <HungryMessage isHungry={true} /> <HungryMessage isHungry={false} /> </div> )
推薦寫法:
import React from 'react' const HungryMessage = ({ isHungry }) => ( <span>{isHungry ? 'I am hungry' : 'I am full'}</span> ) export const BooleanPropGood = () => ( <div> <HungryMessage isHungry /> <HungryMessage isHungry={false} /> </div> )
3.String Props簡(jiǎn)寫
personName處簡(jiǎn)寫了
不推薦寫法:
import React from 'react' const Greeting = ({ personName }) => <p>Hi, {personName}!</p> export const StringPropValuesBad = () => ( <div> <Greeting personName={"John"} /> <Greeting personName={'Matt'} /> <Greeting personName={`Paul`} /> </div> )
推薦寫法:
import React from 'react' const Greeting = ({ personName }) => <p>Hi, {personName}!</p> export const StringPropValuesGood = () => ( <div> <Greeting personName="John" /> <Greeting personName="Matt" /> <Greeting personName="Paul" /> </div> )
4.事件處理函數(shù)簡(jiǎn)寫
onChange處簡(jiǎn)寫了
不推薦寫法:
import React, { useState } from 'react' export const UnnecessaryAnonymousFunctionsBad = () => { const [inputValue, setInputValue] = useState('') const handleChange = e => { setInputValue(e.target.value) } return ( <> <label htmlFor="name">Name: </label> <input id="name" value={inputValue} onChange={e => handleChange(e)} /> </> ) }
推薦寫法:
import React, { useState } from 'react' export const UnnecessaryAnonymousFunctionsGood = () => { const [inputValue, setInputValue] = useState('') const handleChange = e => { setInputValue(e.target.value) } return ( <> <label htmlFor="name">Name: </label> <input id="name" value={inputValue} onChange={handleChange} /> </> ) }
5.組件作為參數(shù)返回
IconComponent處簡(jiǎn)寫了
不推薦寫法:
import React from 'react' const CircleIcon = () => ( <svg height="100" width="100"> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> </svg> ) const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( <div> <IconComponent /> </div> ) export const UnnecessaryAnonymousFunctionComponentsBad = () => ( <ComponentThatAcceptsAnIcon IconComponent={() => <CircleIcon />} /> )
推薦寫法:
import React from 'react' const CircleIcon = () => ( <svg height="100" width="100"> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> </svg> ) const ComponentThatAcceptsAnIcon = ({ IconComponent }) => ( <div> <IconComponent /> </div> ) export const UnnecessaryAnonymousFunctionComponentsGood = () => ( <ComponentThatAcceptsAnIcon IconComponent={CircleIcon} /> )
6.設(shè)置依賴于先前pros的pros
如果新狀態(tài)依賴于先前狀態(tài),則始終將狀態(tài)設(shè)置為先前狀態(tài)的函數(shù)??梢耘幚鞷eact狀態(tài)更新,并且不以這種方式編寫更新會(huì)導(dǎo)致意外結(jié)果,setIsDisabled處簡(jiǎn)寫
不推薦寫法:
import React, { useState } from 'react' export const PreviousStateBad = () => { const [isDisabled, setIsDisabled] = useState(false) const toggleButton = () => setIsDisabled(!isDisabled) const toggleButton2Times = () => { for (let i = 0; i < 2; i++) { toggleButton() } } return ( <div> <button disabled={isDisabled}> I'm {isDisabled ? 'disabled' : 'enabled'} </button> <button onClick={toggleButton}>切換按鈕狀態(tài)</button> <button onClick={toggleButton2Times}>切換按鈕狀態(tài)2次</button> </div> ) }
推薦寫法:
import React, { useState } from 'react' export const PreviousStateGood = () => { const [isDisabled, setIsDisabled] = useState(false) const toggleButton = () => setIsDisabled(isDisabled => !isDisabled) const toggleButton2Times = () => { for (let i = 0; i < 2; i++) { toggleButton() } } return ( <div> <button disabled={isDisabled}> I'm {isDisabled ? 'disabled' : 'enabled'} </button> <button onClick={toggleButton}>切換按鈕狀態(tài)</button> <button onClick={toggleButton2Times}>切換按鈕狀態(tài)2次</button> </div> ) }
感謝各位的閱讀,以上就是“React應(yīng)該學(xué)會(huì)的開發(fā)技巧有哪些”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)React應(yīng)該學(xué)會(huì)的開發(fā)技巧有哪些這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
分享標(biāo)題:React應(yīng)該學(xué)會(huì)的開發(fā)技巧有哪些
鏈接分享:http://sd-ha.com/article12/popcdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、服務(wù)器托管、靜態(tài)網(wǎng)站、域名注冊(cè)、網(wǎng)站排名、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)