In React, a controlled component refers to a component that updates the current value through a callback function; the React component that renders the form also controls the operations that occur on the form during user input, whenever the state of the form changes. , will be written to the state of the component. This type of component is called a controlled component in React.
The operating environment of this tutorial: Windows 10 system, react17.0.1 version, Dell G3 computer.
What is react controlled component
The React component that renders the form also controls the operations that occur on the form during user input. Form input elements whose values are controlled by React in this way are called "controlled components".
Some netizens explained this: In React, whenever the state of the form changes, it will be written to the state of the component. This type of component is called a controlled component in React.
Update process of controlled components:
1, you can set the default value of the form in the initial state
2. Whenever the value of the form changes, call the onChange event handler,
3. The event handler gets the changed state through the event object e, and changes the state;
4, setState triggers the view update and completes the update of the form component value
The controlled component updates the current value through a callback function, such as OnChange . The parent component controls and manages its state through callback functions and passes new values to it as properties. Controlled components are also called "dumb components".
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} />; }
Expand knowledge:
What are uncontrolled components?
Uncontrolled components are components that store their own state internally. You can use ref to query the DOM to find its current value when needed. Kind of like traditional HTML. Most native React form components support both controlled and uncontrolled:
const { useRef } from 'react'; function Example () { const inputRef = useRef(null); return <input type="text" defaultValue="bar" ref={inputRef} /> }
4 What is the difference between them?
In controlled components, form data is processed by React components. In uncontrolled components, the form data is handled by the DOM itself.
For controlled components, component state must be used. For uncontrolled components, the use of state is completely optional, but Refs must be used within them.
For controlled components, we can validate on input, but not for uncontrolled components.
【Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of what are react controlled components. For more information, please follow other related articles on the PHP Chinese website!

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

HTML5aimstoenhancewebcapabilities,makingitmoredynamic,interactive,andaccessible.1)Itsupportsmultimediaelementslikeand,eliminatingtheneedforplugins.2)Semanticelementsimproveaccessibilityandcodereadability.3)Featureslikeenablepowerful,responsivewebappl

HTML5aimstoenhancewebdevelopmentanduserexperiencethroughsemanticstructure,multimediaintegration,andperformanceimprovements.1)Semanticelementslike,,,andimprovereadabilityandaccessibility.2)andtagsallowseamlessmultimediaembeddingwithoutplugins.3)Featur

HTML5isnotinherentlyinsecure,butitsfeaturescanleadtosecurityrisksifmisusedorimproperlyimplemented.1)Usethesandboxattributeiniframestocontrolembeddedcontentandpreventvulnerabilitieslikeclickjacking.2)AvoidstoringsensitivedatainWebStorageduetoitsaccess

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

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
