How to use the enablePatches function from immer
Find comprehensive JavaScript immer.enablePatches code examples handpicked from public code repositorys.
immer.enablePatches is a function in the Immer library that enables the generation of patches when modifying an object with Immer.
0 1 2 3 4 5 6 7 8 9 10
//https://snyk.io/vuln/SNYK-JS-IMMER-1019369 test("prototype pollution in immer", () => { expect({}.polluted).toBe(undefined); const { applyPatches, enablePatches } = require("immer"); enablePatches(); applyPatches({}, [ { op: "add", path: ["__proto__", "polluted"], value: "yes" }, ]);
+ 8 other calls in file
How does immer.enablePatches work?
immer.enablePatches is a function in the Immer library that enables the generation of patches when modifying an object with Immer. By default, when you modify an object with Immer, it creates a new copy of the object and returns it, while leaving the original object unchanged. This is known as "structural sharing," and it allows Immer to make modifications to an object in a performant way without mutating the original object. However, sometimes you may want to track the changes made to an object so that you can later apply them to another object. This is where immer.enablePatches comes in. When you call immer.enablePatches(), Immer will start tracking the changes made to objects using "patches." A patch is a plain JavaScript object that describes a modification to an object, such as a property being added, changed, or removed. After enabling patches, you can modify an object as usual using Immer, and Immer will generate a set of patches describing the modifications made to the object. You can access the patches using the patches property of the Immer Proxy object. You can then apply these patches to another object using the immer.applyPatches() function, which takes an object and an array of patches and applies the patches to the object, returning the modified object. Overall, immer.enablePatches provides a way to track changes made to an object using Immer, allowing you to later apply those changes to another object. This can be useful in situations where you need to synchronize data between clients or store a history of changes made to an object.
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
const { produce, enablePatches, applyPatches } = require("immer"); // Enable patches enablePatches(); // Create an object const originalObject = { foo: { bar: 1, }, baz: [2, 3, 4], }; // Modify the object using Immer const modifiedObject = produce(originalObject, (draft) => { draft.foo.bar = 2; draft.baz.push(5); }); // Access the patches generated by Immer const patches = modifiedObject.__internal_patch; console.log(patches); // Output: [ // { op: "replace", path: [ "foo", "bar" ], value: 2 }, // { op: "add", path: [ "baz", "3" ], value: 5 } // ] // Apply the patches to a new object const newObject = {}; applyPatches(newObject, patches); console.log(newObject); // Output: { // foo: { // bar: 2 // }, // baz: [2, 3, 4, 5] // }
In this example, we use immer.enablePatches() to enable the generation of patches when modifying an object with Immer. We then create an object originalObject and modify it using Immer, which generates a set of patches describing the modifications made to the object. We access the patches generated by Immer using the __internal_patch property of the modifiedObject, which is returned by the produce() function. We then apply these patches to a new object newObject using immer.applyPatches(), which returns the modified object. By using immer.enablePatches() and immer.applyPatches(), we can track the changes made to an object using Immer and apply those changes to another object, providing a flexible way to synchronize data between clients or store a history of changes made to an object.
immer.produce is the most popular function in immer (552 examples)