Skip to content

Proposal: Add trivial dispatch mapping functionalityΒ #2

@raunofreiberg

Description

@raunofreiberg

Loving this library and it's simplicity so far. I'm just wondering why have you decided to expose dispatch directly instead of adding some trivial mapping of dispatch.

What I would love to see is:

  • useSubstate takes a secondary argument actions which is either a object or a function
  • Based on the type, call the actions with dispatch or simply bind the actions.
const mappedActions =
    typeof actions === "function"
      ? actions(store.dispatch)
      : bindActionCreators(actions, store.dispatch);
  • Return it as return [substate, mappedActions];

Would love to hear your feedback on this :-) This doesn't seem to complicate the implementation that much and just allows you to write out your actions directly in the hook itself.

E.g:

const [substate, actions] = useSubstate(
    state => ({ count: state.count }),
    dispatch => ({
      increment: () => dispatch(increment()),
      decrement: () => dispatch(decrement()),
    })
  );

or

const [substate, actions] = useSubstate(
    state => ({ count: state.count }),
    { increment, decrement }
  );

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions