I have created dynamic fields from JSON data, and I am successfully rendering on UI
- Initially all the fields are disabled.
- Once I click on edit I am making particular row editable which is working fine
- On click of cancel what I want to do is make the fields disabled again and it should take the previous (initial value)
Issue
When I click on cancel I am setting the initial data aging but it is not taking, I am using react-form-hook
for form validation, there we have reset()
function but that too is not working.
What I am doing is
Getting data from main component and setting it to some state variable like below
useEffect(() => {
if (li) {
setdisplayData(li);
setCancelData(li);
}
}, [li]);
Now using
displayData
to render the elementsOn click of
Edit
I am doing thisconst Edit = () => { setdisabled(false); };
and on click of cancel I am doing below
const cancel = () => {
setdisabled(true); //disbaled true
console.log(cancelData);
setdisplayData(cancelData); setting my main data back to previous one
reset(); // tried this reset of react hook form but it did not work
};
I am using defaultValue
so that when I click on Edit the field should allow me to edit.