@matt-tingen/rtk-producers
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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.

Dependents (0)

Package Sidebar

Install

npm i @matt-tingen/rtk-producers

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

16.4 kB

Total Files

14

Last publish

Collaborators

  • matt-tingen