- Forums
- react
- V2 - React useState update - React Update Values Of Nested Object onChange
This is version 2 to update form field on change for an object in usestate react [5196], Last Updated: Mon Jun 24, 2024
cathy
Sat Sep 02, 2023
0 Comments
331 Visits
Use the following code to update form fields with OnChange().
1. Declare the variable
const [form, setForm] = useState(
{
mytextarea: { type: "textarea", title: "Your Name", name: "name", value: "This is the text area default value" },
}
);
2. Use the varilable in JSX - Call on the generateField() function whe OnChange to update form.mytextarea.value
{
return <textarea
className="form-control"
rows={4}
cols={40}
name={form[fieldName].name}
onChange={(e) => setForm(
{ ...form, mytextarea : {
...form.mytextarea,
value: e.target.value
}
})}
>{form[fieldName].value}</textarea>