Skip to content

Commit

Permalink
json patch in-place breaks some components
Browse files Browse the repository at this point in the history
this is because React is checking object identity to
determine if changes need to be applied. to resolve this
we deep copy the subsection of the model which is changing
to trigger updates
  • Loading branch information
rmorshea committed Dec 21, 2020
1 parent 310e749 commit a30ef8b
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions idom/client/app/core_modules/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,34 @@ function useInplaceJsonPatch(doc) {

const applyPatch = react.useCallback(
(path, patch) => {
jsonpatch.applyPatch(
jsonpatch.getValueByPointer(ref.current, path),
patch
);
applyPatchInplace(ref.current, path, patch);
forceUpdate();
},
[ref, forceUpdate]
);

return [ref.current, applyPatch];
}

function applyPatchInplace(doc, path, patch) {
if (!path) {
jsonpatch.applyPatch(doc, patch);
} else {
jsonpatch.applyPatch(doc, [
{
op: "replace",
path: path,
value: jsonpatch.applyPatch(
jsonpatch.getValueByPointer(doc, path),
patch,
false,
false
).newDocument,
},
]);
}
}

function useForceUpdate() {
const [, updateState] = react.useState();
return react.useCallback(() => updateState({}), []);
Expand Down

0 comments on commit a30ef8b

Please sign in to comment.