1.0.0 • Published 2 years ago

@matt-tingen/rtk-producers v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

createCaseReducers is a helper to reduce the overhead in defining case reducers for @reduxjs/toolkit slices, particularly when there is code shared between case reducers.

Example usage:

interface FooState {
  values: number[];
  selectedIndex?: number;
}

const initialState: FooState = { values: [] };

const buildProducers = (state: FooState) => {
  const deselect = () => {
    state.selectedIndex = undefined;
  };

  const select = (index: number) => {
    state.selectedIndex = index;
  };

  const appendValue = (value: number) => {
    state.values.push(value);
    select(state.values.length - 1);
  };

  return {
    deselect,
    select,
    appendValue,
  };
};

const fooSlice = createSlice({
  name: 'foo',
  initialState,
  reducers: createCaseReducers(buildProducers, initialState),
});

The producers factory is called each time an action is dispatched which could potentially have a performance impact if actions are dispatched rapidly. This has not been thoroughly tested.