How to use the immer.createDraft 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 ianstormtaylor / slate / packages / slate / src / transforms / general.ts View on Github external
transform(editor: Editor, op: Operation) {
    editor.children = createDraft(editor.children)
    let selection = editor.selection && createDraft(editor.selection)

    switch (op.type) {
      case 'insert_node': {
        const { path, node } = op
        const parent = Node.parent(editor, path)
        const index = path[path.length - 1]
        parent.children.splice(index, 0, node)

        if (selection) {
          for (const [point, key] of Range.points(selection)) {
            selection[key] = Point.transform(point, op)!
          }
        }

        break
github ianstormtaylor / slate / packages / slate / src / interfaces / editor.ts View on Github external
transform(editor: Editor, op: Operation) {
    editor.children = createDraft(editor.children)
    let selection = editor.selection && createDraft(editor.selection)

    switch (op.type) {
      case 'insert_node': {
        const { path, node } = op
        const parent = Node.parent(editor, path)
        const index = path[path.length - 1]
        parent.children.splice(index, 0, node)

        if (selection) {
          for (const [point, key] of Range.points(selection)) {
            selection[key] = Point.transform(point, op)!
          }
        }

        break
      }
github ianstormtaylor / slate / packages / slate / src / interfaces / editor.ts View on Github external
transform(editor: Editor, op: Operation) {
    editor.children = createDraft(editor.children)
    let selection = editor.selection && createDraft(editor.selection)

    switch (op.type) {
      case 'insert_node': {
        const { path, node } = op
        const parent = Node.parent(editor, path)
        const index = path[path.length - 1]
        parent.children.splice(index, 0, node)

        if (selection) {
          for (const [point, key] of Range.points(selection)) {
            selection[key] = Point.transform(point, op)!
          }
        }

        break
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);
      }
    }
github MynockSpit / no-boilerplate-redux / src / produce.js View on Github external
function safeCreateDraft(state) {
  let draft

  try {
    draft = createDraft(state)
  }

  catch (error) {
    if (error.message !== 'First argument to `createDraft` must be a plain object, an array, or an immerable object') {
      throw error
    }

    draft = state
  }

  return draft
}
github kalmhq / kalm / frontend / src / reducers / tutorial.ts View on Github external
const reducer = produce((state: State, action: Actions) => {
  if (action.type === OPEN_TUTORIAL_DRAWER) {
    state.drawerOpen = true;
    return;
  }

  if (action.type === CLOSE_TUTORIAL_DRAWER) {
    state.drawerOpen = false;
    return;
  }

  if (action.type === RESET_TUTORIAL_ACTION) {
    const drawerOpen = state.drawerOpen;
    state = createDraft(initialState);
    state.drawerOpen = drawerOpen;
    return state;
  }

  if (action.type === SET_TUTORIAL_ACTION) {
    const drawerOpen = state.drawerOpen;
    state = createDraft(initialState);
    state.drawerOpen = drawerOpen;
    state.tutorial = action.payload.tutorial;
    state.currentStepIndex = 0;
    return state;
  }

  if (action.type === SET_TUTORIAL_FORM_VALUES) {
    if (state.formValues) {
      state.formValues[action.payload.form] = action.payload.values;
github kalmhq / kalm / frontend / src / reducers / tutorial.ts View on Github external
if (action.type === CLOSE_TUTORIAL_DRAWER) {
    state.drawerOpen = false;
    return;
  }

  if (action.type === RESET_TUTORIAL_ACTION) {
    const drawerOpen = state.drawerOpen;
    state = createDraft(initialState);
    state.drawerOpen = drawerOpen;
    return state;
  }

  if (action.type === SET_TUTORIAL_ACTION) {
    const drawerOpen = state.drawerOpen;
    state = createDraft(initialState);
    state.drawerOpen = drawerOpen;
    state.tutorial = action.payload.tutorial;
    state.currentStepIndex = 0;
    return state;
  }

  if (action.type === SET_TUTORIAL_FORM_VALUES) {
    if (state.formValues) {
      state.formValues[action.payload.form] = action.payload.values;
    } else {
      state.formValues = {
        [action.payload.form]: action.payload.values,
      };
    }
    return;
  }

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