immer#isDraft TypeScript Examples

The following examples show how to use immer#isDraft. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From firebase-tools-ui with Apache License 2.0 7 votes vote down vote up
// Set parent[key] to newValue unless exising value is deeply equal.
// Can be used to set an immer draft property to a nested external object.
export function replaceIfChanged<Parent, Key extends keyof Parent>(
  parent: Parent,
  key: Key,
  newValue: Parent[Key]
): void {
  const oldValue = isDraft(parent) ? original(parent)![key] : parent[key];
  if (!deepEquals(oldValue, newValue)) {
    parent[key] = newValue;
  }
}