Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Forms and Controlled Components

Forms and Controlled Components in React.js

Forms are a fundamental part of most web applications, and React provides a way to work with them efficiently through controlled components. Controlled components allow React to manage the form's state, making it easy to capture and update user input. Here's an overview of forms and controlled components in React:

1. Basic Form Element:

  • In React, you can create form elements like <input>, <textarea>, and <select> as you would in HTML.
      <input type="text" />

 

2. Controlled Components:

  • In React, form elements are controlled components, meaning their value is controlled by the component's state.
  • You store the input value in the component's state and update it when the user interacts with the input.

3. State Initialization:

  • Initialize the component's state to store the form input value. You can use the useState hook for functional components or the state property for class components.
// Functional Component
const [inputValue, setInputValue] = useState('');

// Class Component
this.state = { inputValue: '' };
       

 

 

4. Event Handling:

  • Attach an onChange event handler to the form element. This handler updates the component's state with the new input value.
// Functional Component
const handleChange = (e) => {
  setInputValue(e.target.value);
};

// Class Component
handleChange = (e) => {
  this.setState({ inputValue: e.target.value });
};
        

 

 

5. Value Binding:

  • Bind the input element's value attribute to the component's state. This ties the input value to the state, creating a controlled component.
// Functional Component


// Class Component

       

 

 

6. Form Submission:

  • To handle form submission, attach an onSubmit event handler to the <form> element.
  • Prevent the default form submission behavior using e.preventDefault().
  • You can access the form input value from the component's state when the form is submitted.
// Functional Component
const handleSubmit = (e) => {
  e.preventDefault();
  console.log('Submitted: ', inputValue);
};

// Class Component
handleSubmit = (e) => {
  e.preventDefault();
  console.log('Submitted: ', this.state.inputValue);
};
       

 

 

7. Handling Multiple Inputs:

  • For forms with multiple input elements, create a separate state variable for each input.
const [name, setName] = useState('');
const [email, setEmail] = useState('');
        

 

 

8. Controlled Components for Select Elements:

  • For <select> elements, set the value attribute on the <select> and the value attribute on each <option>.
  • Handle the onChange event to update the selected value in the component's state.

    

 

 

By using controlled components in React, you can easily manage and validate form data, react to user input, and control the submission process. Controlled components are a powerful pattern that helps keep the form state in sync with the UI.