Zettelkasten
week01
What Are Controlled Components

#content


Introduction


Forms are an essential part of most websites and applications. They allow users to interact with your application and provide valuable information. In React, there are two types of form components: controlled and uncontrolled.

Controlled components


Controlled components are those in which React controls the state of the form elements. This means that the value of the form elements is stored in the React state and updated whenever the user changes the value of an element.

To create a controlled component, you need to use the value prop to set the value of the form element and the onChange prop to handle changes made to the value. The value prop defines the initial value of the form element, while the onChange prop is triggered whenever the value of the form element changes.

Uncontrolled components


Uncontrolled components are those in which the DOM controls the state of the form elements. This means that the value of the form elements is stored in the DOM and updated whenever the user changes the value of an element.

To create an uncontrolled component, you do not need to use any special props. Simply render the form element as you would normally.

Advantages of controlled components


Controlled components offer several advantages over uncontrolled components. Some of these advantages include:

  • More control over the form state: With controlled components, you have complete control over the state of the form at any time. This makes it easier to validate the form data and implement complex logic.

  • Better performance: Controlled components generally offer better performance than uncontrolled components, as React can avoid re-rendering the form every time the user changes the value of an element.

  • Easier to test: Controlled components are easier to test than uncontrolled components, as you can control the state of the form in your tests.

Conclusion


Controlled components are the best way to create forms in React. They offer more control over the form state, better performance, and are easier to test.

Examples of controlled components

// Controlled text input component
const TextInput = ({ value, onChange }) => {
  return <input type="text" value={value} onChange={onChange} />;
};
 
// Controlled select component
const Select = ({ value, options, onChange }) => {
  return (
    <select value={value} onChange={onChange}>
      {options.map((option) => (
        <option key={option.value} value={option.value}>
          {option.label}
        </option>
      ))}
    </select>
  );
};
 
// Controlled form component
const Form = ({ onSubmit }) => {
  const [name, setName] = useState('');
  const [email, setEmail] = useState('');
 
  const handleNameChange = (event) => {
    setName(event.target.value);
  };
 
  const handleEmailChange = (event) => {
    setEmail(event.target.value);
  };
 
  const handleSubmit = (event) => {
    // Validate the form data
    // Submit the form data to the server
 
    event.preventDefault();
  };
 
  return (
    <form onSubmit={handleSubmit}>
      <TextInput label="Name" value={name} onChange={handleNameChange} />
      <TextInput label="Email" value={email} onChange={handleEmailChange} />
      <button type="submit">Submit</button>
    </form>
  );
};