Adding Forms in React:-
Forms play an essential role in modern web applications. They enable users to share information, complete tasks and provide feedback. In React, there are two ways of handling form data:-
1. Controlled Components: In this approach, form data is handled by React through the use of hooks such as the
2. Uncontrolled Components: Form data is handled by the Document Object Model (DOM) rather than by React. The DOM maintains the state of form data and updates it based on user input.
Controlled Components in React:-
In React, a controlled component is a component where form elements derive their value from a React state.
When a component is controlled, the value of form elements is stored in a state, and any changes made to the value are immediately reflected in the state
value prop sets the initial value of a form element, while the
onChange event is triggered whenever the value of a form element changes. Inside the
onChange event, you need to update the state with the new value using a state update function.
Uncontrolled Components in React:-
Uncontrolled components in React refer to form elements whose state is not managed by React. Instead, their state is handled by the browser’s DOM.
In contrast, an uncontrolled component allows the browser to handle the form elements’ state. When a user enters text into a text input field or selects an option from a select box, the browser updates the DOM’s state for that element automatically.
Here’s an example of an uncontrolled component:
In this example:
We have a form that contains a text input field, a select box, and a checkbox. Instead of creating state for each form element and writing event handlers to update the state, we’re using uncontrolled components. This means that the browser is responsible for managing the state of the form elements.
When a user interacts with a form element, the browser automatically updates the DOM’s state for that element. And to retrieve the current values of each form element, we’re using the
Uncontrolled components can be useful in certain situations, such as when you need to integrate with third-party libraries or when you don’t need to manipulate the form data.
React Hook Form:-
React Hook Form is a lightweight library for managing forms in React applications. Whether you need to create a simple contact form or a complex multistep form, React Hook Form can help simplify your form creation process.
Installation = You can do this using
npm by running the following command:
npm install react-hook-form
By importing the
useForm hook, you can start using React Hook Form to manage forms in your application.
useForm hook provides several functions and properties that you can use to manage your form:
register: This function is used to register form fields with React Hook Form.
handleSubmit: This is used to handle form submissions. It takes a callback function that is called when the form is submitted.
errors: This represents an object containing any validation errors that occur when a form is submitted.
watch: This function is used to watch for changes to specific form fields. It takes an array of form field names and returns the current value of those fields.
By following the above steps, you can create a React Form with so much ease.