How to Reset Dirty State for Nested Arrays and Objects Without Affecting Other Fields? #12620
Unanswered
zain-bin-ramzan
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am working on a multi-step form using React Hook Form, and I need to reset the dirty state of specific fields dynamically.
Form Structure:
Each question contains:
text
field (primitive type).options
array, where each option contains:videos
array.text
field.Each question is saved individually, and after saving, I want to reset its dirty state without affecting the rest of the form.
Example Form Data:
On my UI, each question has its own Save button. When saving, I want to clear the dirty fields for that specific question only.
Issue:
I created a recursive function using
resetField()
to reset only the dirty fields of the saved question.✅ It works for primitive fields.
❌ But it does not reset the dirty state for arrays and objects.
Function Attempt:
What I Have Tried:
1️⃣ Adding logs to verify that the function correctly processes the nested fields ✅
2️⃣ Using
resetField(path, { keepDirty: false })
explicitly ✅3️⃣ Using
setValue(path, watch(path))
before resetting ✅4️⃣
resetField()
works fine for primitive fields, but does not clear arrays or objects ❌Expected Behavior:
When I save a question, only that question’s fields (including nested options and videos) should have their dirty state reset, without affecting other questions or fields in the form.
Question:
resetField()
without affecting other fields?Additional Utility Function (For Dirty Values Extraction)
I am using the following function to get dirty values:
Beta Was this translation helpful? Give feedback.
All reactions