The difference is who owns the form value: React state (controlled) or the DOM itself (uncontrolled). Controlled components - The input's value comes from React state, and every change goes through an onChange handler that updates the state. - React is the single source of truth. function SignupForm() { const [email, setEmail] = useState(''); const isValid = email.includes('@'); return ( <form onSubmit={e => { e.preventDefault(); submit(email); }}> <input type="email" value={email} onChange={e => setEmail(e.target.value)} /> {!isValid && email && <p role="alert">Enter a valid email</p>} <button disabled={!isValid}>Sign up</button> </form> ); } Advantages - Instant validation and feedback as the user types. - Easy to enforce formats (uppercase, masks), enable or disable buttons, and derive other UI from values. - Values are always available in state for submission or sharing with other components. Disadvantages - More code, and a re-render on every keystroke (usually fine; can matter in very large forms). Uncontrolled components - The DOM keeps the value; React reads it when needed, usually through a ref or the form submission event. - You can set an initial value with defaultValue (or defaultChecked). function SignupFormUncontrolled() { const emailRef = useRef<HTMLInputElement>(null); return ( <form onSubmit={e => { e.preventDefault(); submit(emailRef.current!.value); }}> <input type="email" defaultValue="" ref={emailRef} /> <button>Sign up</button> </form> ); } Or read all fields with FormData: <form onSubmit={e => { e.preventDefault(); const data = new FormData(e.currentTarget); submit(data.get('email')); }}> Advantages - Less code and fewer re-renders; works well for simple forms. - Natural fit for file inputs: <input type="file"> is always uncontrolled because its value is read-only. - Easier integration with non-React libraries that manipulate the DOM. Disadvantages - Harder to validate on every change or react to values in real time. Key differences - Source of truth: React state vs the DOM. - Value props: value plus onChange vs defaultValue plus ref or FormData. - Re-renders: on every change vs none while typing. - Real-time validation: easy vs harder. Common mistake Switching between controlled and uncontrolled — for example, starting with value={undefined} and later setting a string — triggers a React warning. Initialise state with an empty string, not undefined. Modern practice - Form libraries such as React Hook Form use uncontrolled inputs with refs internally for performance, while still providing validation (often with zod schemas). - React 19 adds form Actions (<form action={fn}>), useActionState and useFormStatus, which work naturally with uncontrolled inputs and FormData, especially with server actions in frameworks such as Next.js. Interview summary: "Controlled components keep input values in React state and update them through onChange, giving full control and live validation. Uncontrolled components let the DOM hold the value and read it via refs or FormData, which is simpler and faster for basic forms and required for file inputs."
Ravindra Wadile · · 0 likes