redux-zero / redux-zero

A lightweight state container based on Redux

Home Page:https://matheusml1.gitbooks.io/redux-zero-docs/content/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Combining Actions

jetako opened this issue · comments

Here's a simple utility function I'm using that merges multiple action maps together. Thought this might be worthy of adding to the core library, unless I'm missing an better approach:

export function combineActions(...actionsArray) {
  return store => (
    actionsArray.reduce((acc, actions) => {
      actions = typeof actions === "function" ? actions(store) : actions
      return Object.assign(acc, actions)
    }, {})
  )
}

Usage:

import { userActions, todoActions } from '../actions'

@connect((state, props) => {
  return state
}, combineActions(userActions, todoActions))
export class App extends Component {
  ...
}

Hi @jetako

This surely can be useful. Can you add a PR with this?

@jetako there's a clean way to do this, without the need of a function. We can simply use the spread operator, like this:

import { connect } from "redux-zero/react";

import Component from "./Component";
import firstActions from "../../actions/firstActions";
import secondActions from "../../actions/secondActions";

export default connect(
  ({ params, moreParams }) => ({ params, moreParams }),
  (...actionParams) => ({
    ...firstActions(...actionParams),
    ...secondActions(...actionParams)
  })
)(Component);

I'm adding this to the DOCs.

Hello, @matheusml

I've made a function to combine actions inspired on your example:

import { connect } from "redux-zero/react";

import Component from "./Component";
import firstActions from "../../actions/firstActions";
import secondActions from "../../actions/secondActions";

const combineActions = (...actions) => (...actionsParams) =>
  actions.reduce((acc, action) => ({ ...acc, ...action(...actionsParams) }), {});

export default connect(
  ({ params, moreParams }) => ({ params, moreParams }),
  combineActions(firstActions, secondActions)
)(Component);

I think that would be cool have a funciton like this on redux-zero/utils. What do you think?

Ok, @leobetosouza

Please add this.

@leobetosouza, It looks like your version won't work with simple object actions, hence why my proposal checks for a "function" type.

A little tip: { ...acc, ...action(...actionsParams) } is doing more work than necessary. You are creating a new object in each loop, when you only need to assign to acc.
Object.assign(acc, action(...actionsParams)) is more efficient.

@leobetosouza before I can accept the PR, could you please take into account @jetako 's comments?

@jetako

A little tip: { ...acc, ...action(...actionsParams) } is doing more work than necessary. You are creating a new object in each loop, when you only need to assign to acc.
Object.assign(acc, action(...actionsParams)) is more efficient.

It's proposital. I'm trying to work only with pure functions.

It looks like your version won't work with simple object actions, hence why my proposal checks for a "function" type.

@jetako Yeah, I missed it. You're right. I haven't noticed that we have simple object actions. I will work on it tomorrow.

@jetako @matheusml i've updated the PR (and tests) with actions object consideration :)