JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext

Learn the concept

Forms

react
mid
forms

What is the difference between controlled and uncontrolled components in React forms?

forms
controlled
uncontrolled
inputs
refs
Quick Answer

Controlled components have their form data managed by React state - the input value is always driven by state. Uncontrolled components let the DOM handle form data using refs to access values when needed. Controlled components are recommended for most cases.

Detailed Explanation

Controlled Components:

  • React state is the 'single source of truth'
  • Every state mutation has an associated handler
  • Input value always reflects state
  • Form data is instantly accessible
  • Enables validation, conditional disabling, enforced formats

Uncontrolled Components:

  • DOM maintains its own state
  • Use refs to access values
  • Like traditional HTML forms
  • Less code for simple cases
  • Useful for integrating non-React code

When to Use Each:

  • Controlled: Most forms, validation, instant feedback
  • Uncontrolled: Simple forms, file inputs, third-party integrations

React 19 Form Actions:

  • <form action={fn}> — pass a function directly to form's action prop
  • useActionState — tracks form state (pending, error, result)
  • useFormStatus — access pending state from form children
  • Works seamlessly with Server Actions for server-side processing

Code Examples

Controlled componentJSX
function ControlledForm() {
  const [formData, setFormData] = useState({
    name: '',
    email: '',
    agree: false
  });
  const [errors, setErrors] = useState({});
  
  const handleChange = (e) => {
    const { name, value, type, checked } = e.target;
    setFormData(prev => ({
      ...prev,
      [name]: type === 'checkbox' ? checked : value
    }));
  };
  
  const validate = () => {
    const newErrors = {};
    if (!formData.name) newErrors.name = 'Name required';
    if (!formData.email.includes('@')) newErrors.email = 'Invalid email';
    setErrors(newErrors);
    return Object.keys(newErrors).length === 0;
  };
  
  const handleSubmit = (e) => {
    e.preventDefault();
    if (validate()) {
      console.log('Submit:', formData);
    }
  };
  
  return (
    <form onSubmit={handleSubmit}>
      <input
        name="name"
        value={formData.name}
        onChange={handleChange}
      />
      {errors.name && <span>{errors.name}</span>}
      
      <input
        name="email"
        value={formData.email}
        onChange={handleChange}
      />
      
      <input
        type="checkbox"
        name="agree"
        checked={formData.agree}
        onChange={handleChange}
      />
      
      <button disabled={!formData.agree}>Submit</button>
    </form>
  );
}

Real-World Applications

Use Cases

Search with Debounce

Controlled input with debounced API calls for autocomplete/search-as-you-type

File Upload Forms

Uncontrolled file inputs combined with controlled metadata fields in upload forms

Dynamic Form Builder

Controlled forms where field types, validation rules, and layout are driven by configuration

Mini Projects

Multi-Step Checkout Form

intermediate

Build a checkout form with controlled inputs, validation, address autocomplete, and payment fields

Form Builder

advanced

Create a drag-and-drop form builder that generates controlled/uncontrolled forms from JSON schema

Industry Examples

React Hook Form

The most popular form library using uncontrolled inputs with ref-based registration

TanStack Form

Type-safe form library with framework-agnostic core and React adapter

Resources

React Docs - Reacting to Input with State

docs

React Hook Form

docs

Related Questions

What is useRef and when should you use it?

mid
hooks

Explain the useState and useEffect hooks and their common patterns.

mid
hooks
Previous
What is useRef and when should you use it?
Next
How does React Router work and what are its key features?
PrevNext