How to use the immer.produce 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 terotests / ts2redux / src / frontend / models / reducers / SimpleModel.tsx View on Github external
export const SimpleModelReducer = (
  state: ISimpleModel = initSimpleModel(),
  action: any
) => {
  return immer.produce(state, (draft: ISimpleModel) => {
    switch (action.type) {
      case SimpleModelEnums.SimpleModel_items:
        new RSimpleModel(draft).items = action.payload;
        break;
    }
  });
};
/********************************
github terotests / ts2redux / src / frontend / models / reducers / TetrisModel.tsx View on Github external
rotate() {
    const nextState = immer.produce(this.state, (draft: ITetrisModel) =>
      new RTetrisModel(draft).rotate()
    );
    if (this.__devTools) {
      this.__devTools.send("rotate", nextState);
    }
    this.setStateSync(nextState);
  }
  step() {
github terotests / ts2redux / src / frontend / ng.ts View on Github external
export const TestModelReducer = (state:ITestModel = init_TestModel(), action) => {
  return immer.produce(state, draft => {
    switch (action.type) {
      case TestModelEnums.TestModel_items: 
        (new RTestModel(draft)).items = action.payload
        break;
      case TestModelEnums.TestModel_maxId: 
        (new RTestModel(draft)).maxId = action.payload
        break;
      case TestModelEnums.TestModel_shopState: 
        (new RTestModel(draft)).shopState = action.payload
        break;
      case TestModelEnums.TestModel_add: 
        (new RTestModel(draft)).add(action.payload)
        break;
    }
  })
}
github terotests / ts2redux / src / frontend / models / reducers / TodoList.ts View on Github external
export const TodoListReducer = (state:ITodoList = init_TodoList(), action) => {
  return immer.produce(state, draft => {
    switch (action.type) {
      case TodoListEnums.TodoList_items: 
        (new RTodoList(draft)).items = action.payload
        break;
      case TodoListEnums.TodoList_state: 
        (new RTodoList(draft)).state = action.payload
        break;
    }
  })
}
github terotests / ts2redux / src / frontend / models / reducers / TodoList.tsx View on Github external
setTitle(value: string) {
    const nextState = immer.produce(this.state, draft =>
      new RTodoList(draft).setTitle(value)
    );
    if (this.__devTools) {
      this.__devTools.send("setTitle", nextState);
    }
    this.setStateSync(nextState);
  }
  addLotOfItems(cnt: number) {
github facebook / flipper / src / reducers / connections.tsx View on Github external
if (selectedPlugin) {
        performance.mark(`activePlugin-${selectedPlugin}`);
      }

      return updateSelection({
        ...state,
        staticView: null,
        selectedApp: selectedApp || null,
        selectedPlugin,
        userPreferredPlugin: selectedPlugin || state.userPreferredPlugin,
      });
    }

    case 'STAR_PLUGIN': {
      const {selectedPlugin, selectedApp} = action.payload;
      return produce(state, draft => {
        if (!draft.userStarredPlugins[selectedApp]) {
          draft.userStarredPlugins[selectedApp] = [selectedPlugin];
        } else {
          const plugins = draft.userStarredPlugins[selectedApp];
          const idx = plugins.indexOf(selectedPlugin);
          if (idx === -1) {
            plugins.push(selectedPlugin);
          } else {
            plugins.splice(idx, 1);
          }
        }
      });
    }

    case 'SELECT_USER_PREFERRED_PLUGIN': {
      const {payload} = action;
github datenguide / datenguide / src / lib / useSuperRedux.js View on Github external
export const useSuperReducer = (reducers, initialState, id) => {
  const reducer = useCallback(
    (state, action) => {
      if (!reducers[action.type]) {
        throw new Error(`unknown action ${action.type}`)
      }
      return reducers[action.type](state, action)
    },
    [reducers]
  )

  const cachedImmerReducer = useCallback(produce(reducer), [reducer])

  const [reducerState, setReducerState] = useState(initialState, id)

  const state = useRef(reducerState)
  const getState = () => state.current
  const setState = newState => {
    state.current = newState
    setReducerState(newState)
  }

  const reduce = action => cachedImmerReducer(getState(), action)
  const dispatchWithSideEffects = action =>
    typeof action === 'function'
      ? action(dispatchWithSideEffects, getState)
      : setState(reduce(action))
github node-gh / gh / src / cmds / pull-request / index.ts View on Github external
export async function run(options, done) {
    if (!options.repo && !options.all) {
        logger.error('You must specify a Git repository with a GitHub remote to run this command')
    }

    if (!userRanValidFlags(DETAILS.commands, options)) {
        const payload = options.argv.remain && options.argv.remain.concat().slice(1)

        options = produce(options, draft => {
            if (payload && payload[0]) {
                draft.fetch = true
            } else {
                draft.list = true
            }
        })
    }

    const numbers = [...options.number]

    for (const number of numbers) {
        await main(number, options)
    }

    done && done()
github kalmhq / kalm / frontend / src / pages / Application / Log.tsx View on Github external
onClick={(e) => {
                  e.stopPropagation();
                  e.preventDefault();
                  this.handleCloseMore(
                    produce(logOptions, (draft) => {
                      draft[option.key] = !logOptions[option.key];
                    }),
                  );
                }}
              >

immer

Create your next immutable state by mutating the current one

MIT
Latest version published 8 months ago

Package Health Score

84 / 100
Full package analysis