How to use the immer.isDraftable function in immer

To help you get started, we’ve selected a few immer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github knpwrs / redux-ts-utils / src / handle-action.ts View on Github external
return (state: S | undefined, action: ReturnType) => {
    if (action.type === ac.type && state) {
      if (isDraftable(state)) {
        const draft = createDraft(state);
        const reResult = re(draft, action);
        const finishedDraft = finishDraft(draft);

        if (finishedDraft === state && reResult !== undefined) {
          return reResult;
        }

        return finishedDraft;
      }
      // Support primitive-returning reducers
      return re(state as Draft<s>, action);
    }
    return (state || s) as any;
  };
}</s>
github redux-model / redux-model / tests / models / NoImmerModel.spec.ts View on Github external
test('For basic data type, we can not modify state', () => {
  expect(model.data).toBe('foo');
  model.changeData();
  expect(model.data).toBe('bar');
  expect(model.lastState).toBe('foo');
  expect(isDraftable(model.lastState)).toBeFalsy();
});
github redux-model / redux-model / src / core / reducer / BaseReducer.ts View on Github external
protected changeState(effect: Function, state: any, action: ActionResponseHandle | ActionNormalHandle): any {
    if (isDraftable(state)) {
      const draft = createDraft(state);
      const responseDraft = effect(draft, action);

      if (responseDraft === undefined) {
        state = finishDraft(draft);
      } else if (isDraft(responseDraft)) {
        state = finishDraft(responseDraft);
      } else {
        state = responseDraft;
      }
    } else {
      state = effect(state, action);

      if (state === undefined) {
        throw new StateReturnRequiredError(action.type);
      }

immer

Create your next immutable state by mutating the current one

MIT
Latest version published 7 months ago

Package Health Score

93 / 100
Full package analysis