在react中,受控元件指的是透過回呼函數來更新目前值的元件;渲染表單的React元件也控制使用者輸入過程中表單發生的操作,每當表單的狀態改變時,都會被寫入到元件的state中,這種元件在React稱為受控元件。
本教學操作環境:Windows10系統、react17.0.1版、Dell G3電腦。
react受控元件是什麼
渲染表單的 React 元件也控制著使用者輸入過程中表單發生的操作。被 React 以這種方式控制取值的表單輸入元素就叫做「受控元件」。
有網友這樣解釋:在React中,每當表單的狀態改變時,都會被寫入到元件的state中,這種元件在React稱為受控元件。
受控元件的更新流程:
1,可以透過在初始state中設定表單的預設值
2,每當表單的值改變時,呼叫onChange事件處理器,
3,事件處理器透過事件物件e拿到改變後的狀態,改變state;
4,setState觸發視圖更新,完成表單組件值的更新
受控組件是透過透過回調函數來更新目前值,譬如OnChange 。父元件則透過回呼函數控制並管理它的狀態並將新值作為屬性傳遞給它。受控組件也稱為“啞巴組件”。
const { useState } from 'react'; function Controlled () { const [email, setEmail] = useState(); const handleInput = (e) => setEmail(e.target.value); return <input type="text" value={email} onChange={handleInput} />; }
擴充知識:
什麼是不受控元件?
不受控元件則是內部儲存自身狀態的元件,可以使用 ref 查詢 DOM,以便在需要時找到其目前值。有點像傳統的 HTML。大多數原生的 React 表單元件都支援受控和不受控:
const { useRef } from 'react'; function Example () { const inputRef = useRef(null); return <input type="text" defaultValue="bar" ref={inputRef} /> }
4 它們之間有什麼區別?
在受控元件中,表單資料由 React 元件處理。而在不受控元件中,表單資料由 DOM 本身處理。
對於受控元件,必須使用元件狀態。對於不受控制的組件,狀態的使用是完全可選的,但必須在其中使用 Refs 。
對於受控元件,我們可以在輸入時進行驗證,但對於不受控元件則不能進行驗證。
【相關推薦:javascript影片教學、web前端】
以上是react受控元件是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

html5aimstoenhancewebcapabilities,Makeitmoredynamic,互動,可及可訪問。 1)ITSupportsMultimediaElementsLikeAnd,消除innewingtheneedtheneedtheneedforplugins.2)SemanticeLelelemeneLementelementsimproveaCceccessibility inmproveAccessibility andcoderabilitile andcoderability.3)emply.3)lighteppoperable popperappoperable -poseive weepivewebappll

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒體綜合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityActibility.2)and tagsallowsemlessallowseamelesseamlessallowseamelesseamlesseamelesseamemelessmultimedimeDiaiaembediiaembedplugins.3)。 3)3)

html5isnotinerysecure,butitsfeaturescanleadtosecurityrisksifmissusedorimproperlyimplempled.1)usethesand andboxattributeIniframestoconoconoconoContoContoContoContoContoconToconToconToconToconToconTedContDedContentContentPrenerabilnerabilityLikeClickLickLickLickjAckJackJacking.2)

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

使用ID選擇器在CSS中並非固有地不好,但應謹慎使用。 1)ID選擇器適用於唯一元素或JavaScript鉤子。 2)對於一般樣式,應使用類選擇器,因為它們更靈活和可維護。通過平衡ID和類的使用,可以實現更robust和efficient的CSS架構。

html5'sgoalsin2024focusonrefinement和optimization,notNewFeatures.1)增強performanceandeffipedroptimizedRendering.2)inviveAccessibilitywithRefinedwithRefinedTributesAndEllements.3)explityconcerns,尤其是withercercern.4.4)

html5aimedtotoimprovewebdevelopmentInfourKeyAreas:1)多中心供應,2)語義結構,3)formcapabilities.1)offlineandstorageoptions.1)html5intoryements html5introctosements introdements and toctosements and toctosements,簡化了inifyingmediaembedingmediabbeddingingandenhangingusexperience.2)newsements.2)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。