Skip to content
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

[feat] Namespacing actions #170

Open
jschill opened this issue Aug 22, 2019 · 2 comments
Open

[feat] Namespacing actions #170

jschill opened this issue Aug 22, 2019 · 2 comments

Comments

@jschill
Copy link

jschill commented Aug 22, 2019

Hello,

When using actions I see two problems.

  1. If there are alot of actions, they kind of pollute the prop root and cause prop hell.
  2. There's no good way of using two different action objects.
import { actions as itemActions } from 'store/item/actions';
import { actions as authActions } from 'store/auth/actions';

Sure, we could combine those two and merge into one object by borrowing the excellent combineActions from redux-zero
https://github.com/redux-zero/redux-zero/blob/3d0b032c0f3c94757e307673d46e9c52327f3b75/src/utils/combineActions.ts

...
const actions = combineActions(itemActions, authActions);
const Connected = connect('item,auth', actions)(PleaseConnectMe);
...

But problem this time is if both itemActions and authActions have a reset action they will collide.

So to solve these two, would it be an idea to implement some kind of prop namespacing? Without discussing the implementation, wouldn't it be an idea to have the itemActions in the example accessible via this.props.itemActions.actionName instead of this.props.actionName.

Would there be any cons with above aproach?

@jschill
Copy link
Author

jschill commented Aug 22, 2019

I've been playing around with the idea of passing an object to connect where the property is a "namespace" and the value are the actions.

So to pick up the previous example:

...
const actions = {
   'itemActionsFoo': itemActions,
   'authActionsBar': authActions
};
const Connected = connect('item, auth', actions)(PleaseConnectMe);
...

And then in PleaseConnectMe i can do:

this.props.itemActionsFoo.reset();

Any thoughts?

I've made a POC here where I expose a "namespaceConnect" function:
https://github.com/jschill/unistore
Commit: jschill@d1ddc45

@developit
Copy link
Owner

The trick here is that it becomes difficult to memoize mapActionsToProps when it contains nested objects. Maybe something like the select() function that powers mapStateToProps could work here?

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

No branches or pull requests

2 participants