How to use immer

Comprehensive immer code examples:

How to use immer.default:

96
97
98
99
100
101
102
103
104
105
actionKeys.forEach(function (actionKey) {
    var typeKey = namespace + "." + actionKey;
    var reducer = reducers[actionKey];
    reducerMap[typeKey] = (isUseImmer
        ? function (state, payload) {
            return immer_1.default(state, function (draftState) {
                reducer(draftState, payload);
            });
        }
        : reducer);

How to use immer.immerable:

74
75
76
77
78
79
80
81
82
83
        this.loading = {};
    }
    return BaseStore;
}());
exports.BaseStore = BaseStore;
_a = immer_2.immerable;
var Store = (function () {
    function Store() {
    }
    Store.create = function (Clazz) {

How to use immer.enablePatches:

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" },
  ]);

How to use immer.produceWithPatches:

2
3
4
5
6
7
8
9
10
11
const a = [
  {
    name: 1
  }
]
const [newA, patches] = im.produceWithPatches(a, (d) => {
  d[0].name =3
})
console.log('newA: ', newA);
console.log('patches: ', patches);

How to use immer.createDraft:

59
60
61
62
63
64
65
66
67
68
  return function () {
    return listeners["delete"](listener);
  };
}
function dispatch(recipeOrPartial) {
  var draft = immer.createDraft(STATE);
  if (typeof recipeOrPartial === 'function') {
    return PromiseResolve(recipeOrPartial(draft)).then(function () {
      return notify(immer.finishDraft(draft));
    });

How to use immer.applyPatches:

78
79
80
81
82
83
84
85
86
87
        return;
    }

    for (let p of patch.result) p.path = p.path.split('/').filter(e => e !== '');

    setData({ accounts: applyPatches(subsData.accounts, patch.result) });
    processAccounts();
});

ec.connect();

How to use immer.produce:

733
734
735
736
737
738
739
740
741
let _self = this;
const currentPageId = this.state.currentPageId;

if (this.state.appDefinition?.pages[currentPageId].components[componentDefinition.id]) {
  const newDefinition = {
    appDefinition: produce(this.state.appDefinition, (draft) => {
      draft.pages[currentPageId].components[componentDefinition.id].component = componentDefinition.component;
    }),
  };