Skip to content

Feature: Slice helpers #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
neurosnap opened this issue Jan 23, 2019 · 1 comment
Closed

Feature: Slice helpers #92

neurosnap opened this issue Jan 23, 2019 · 1 comment

Comments

@neurosnap
Copy link

neurosnap commented Jan 23, 2019

Slice helpers abstract the need to recreate repetitive reducers/actions, etc. I have found myself creating a lot of the same type of reducers and by creating a thin wrapper around createSlice I'm reducing a ton of boilerplate code.

The primary slices I create often are map and simple assign. I've been using these slices in production for awhile now with great success.

Here are some examples:
https://github.com/neurosnap/robodux#slice-helpers

Map slice:

interface SliceState {
  [key: string]: string;
}
interface State {
  test: SliceState
}

interface Actions {
  addTest: State;
  setTest: State;
  removeTest: string[];
  resetTest: never;
}

const slice = 'test';
const { reducer, actions } = mapSlice<SliceState, Actions, State>(slice);
const state = { 3: 'three' };

store.dispatch(
  actions.addTest({
    1: 'one',
    2: 'two',
  })
);
/* {
  1: 'one',
  2: 'two',
  3: 'three,
} */

store.dispatch(
  actions.setTest({ 4: 'four', 5: 'five', 6: 'six' })
)
/* {
  4: 'four',
  5: 'five',
  6: 'six',
} */

store.dispatch(
  actions.removeTest(['5', '6'])
)
/* {
  4: 'four'
} */

store.dispatch(
  actions.resetTest()
)
// {}

What does everyone think?

I'd be happy to submit a PR to add this feature to RSK.

@markerikson
Copy link
Collaborator

Swinging back around to this. Appreciate the suggestion, but I don't think this is something I want to add at this point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants