-
Is there a direct way to get dirty field values? This is useful when submitting updates to an API endpoint - only transmit over the wire the changed field(s), not the entire object/form.
|
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 25 replies
-
hey @dandv we don't have such mechanism, it's easier to implement at your App level. |
Beta Was this translation helpful? Give feedback.
-
OK, this was pretty easy to implement in a generic way: // Map RHF's dirtyFields over the `data` received by `handleSubmit` and return the changed subset of that data.
export function dirtyValues(dirtyFields: object | boolean, allValues: object): object {
// If *any* item in an array was modified, the entire array must be submitted, because there's no way to indicate
// "placeholders" for unchanged elements. `dirtyFields` is `true` for leaves.
if (dirtyFields === true || Array.isArray(dirtyFields))
return allValues;
// Here, we have an object
return Object.fromEntries(Object.keys(dirtyFields).map(key => [key, dirtyValues(dirtyFields[key], allValues[key])]));
} Code sandbox with tests at https://codesandbox.io/s/ts-jest-34w4x?file=/src/index.ts. I think this would be a useful feature to add to |
Beta Was this translation helpful? Give feedback.
-
the recommend new API is pretty useful in many cases. but I prefer another name to use that is a bit intuitive like |
Beta Was this translation helpful? Give feedback.
-
That an alternate version of helper, which gives you perfect TS autocomplete in your components
|
Beta Was this translation helpful? Give feedback.
-
I think using dirty fields is not enough so I'm proposing a convention to extract only the data that has been modified from the form's state. What do you think about it? |
Beta Was this translation helpful? Give feedback.
-
you can use this if you want just the field swith the data |
Beta Was this translation helpful? Give feedback.
OK, this was pretty easy to implement in a generic way:
Code sandbox w…