Skip to content

Get only the dirty field values #1991

Answered by dandv
dandv asked this question in Q&A
Jun 29, 2020 · 6 comments · 25 replies
Discussion options

You must be logged in to vote

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 w…

Replies: 6 comments 25 replies

Comment options

You must be logged in to vote
2 replies
@platoonic
Comment options

@bluebill1049
Comment options

Comment options

You must be logged in to vote
20 replies
@gremo
Comment options

@pedrollmatias
Comment options

@dannobytes
Comment options

@Szorcu
Comment options

@brian-gates
Comment options

Answer selected by bluebill1049
Comment options

You must be logged in to vote
1 reply
@Moshyfawn
Comment options

Comment options

You must be logged in to vote
2 replies
@lveillard
Comment options

@minmaxdata
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet