Form Handling

Pascual Vila
Frontend Instructor.
In React, form handling is a common task, and it can be done in a controlled or uncontrolled manner. Controlled forms are completely managed by React's state, while uncontrolled forms use refs (ref) to access the value of form elements.
Controlled Forms:
In a controlled form, the value of the form fields is managed by React's state. Every time a field changes, the state is updated and the component re-renders.
{`import React, { useState } from "react";
function Form() {
const [inputValue, setInputValue] = useState("");
const handleChange = (event) => {
setInputValue(event.target.value);
};
const handleSubmit = (event) => {
event.preventDefault();
alert("Form submitted: " + inputValue);
};
return (
<form onSubmit={handleSubmit}>
<input type="text" value={inputValue} onChange={handleChange} />
<button type="submit">Submit</button>
</form>
);
}
export default Form;`}