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

add state to mapDispatchToProps #93

Closed
wants to merge 2 commits into from
Closed

Conversation

tcoopman
Copy link

@tcoopman tcoopman commented Sep 5, 2015

This PR is for discussion.
Recently I have found the need to have the state in mapDispatchToProps.

I have an actionCreator like this:

const addFeature = (some, state) => {
  // do something with state
}

const someActionCreator = (myFunction) => (options) => {
  //
};

And I want to write

const selectActions = (dispatch, state) => ({
  someActionCreator: bindActionCreator(actionCreator(addFeature(state.data1, state.data2), dispatch)
});
export default connect(selectState, selectActions)(MyComponent);

I know there are other options to inject addFeature(state.data1, state.data2) to someActionCreator but I would like to do it without MyComponent knowing about it and this looks like a nice solution.

Any reason why this is a bad idea? Is there a better way?

This PR is for discussion.
Recently I have found the need to have the state in mapDispatchToProps.

I have an actionCreator like this:

```js
const addFeature = (some, state) => {
  // do something with state
}

const someActionCreator = (myFunction) => (options) => (dispatch, getState) => {
  //
};
```

And I want to write
```js
const selectActions = (dispatch, state) => ({
  someActionCreator: bindActionCreator(actionCreator(addFeature(state.data1, state.data2), dispatch)
});
export default connect(selectState, selectActions)(MyComponent);
```


I know there are other options to inject `addFeature(state.data1, state.data2)` to `someActionCreator` but I would like to do it without `MyComponent` knowing about it and this looks like a nice solution.

Any reason why this is a bad idea? Is there a better way?
@tcoopman
Copy link
Author

tcoopman commented Sep 6, 2015

I've been thinking about this some more and I'm wondering if the API of connect can't be simplified.

If we pass state, dispatch and ownProps into mapToProps then you don't need 3 separate methods for connect like there are now. The new API would be connect(mapToProps(state, dispatch, ownProps))

The disadvantage is that it's a bit more work for the user then at the moment but it should be possible to expose the current API on top of this new one.

Thoughts?

@gaearon
Copy link
Contributor

gaearon commented Sep 6, 2015

If we pass state, dispatch and ownProps into mapToProps then you don't need 3 separate methods for connect like there are now. The new API would be connect(mapToProps(state, dispatch, ownProps))

This was discussed in #1. The only reason current API isn't that simple is because it essentially makes worst perf case ("rebind everything on every prop change") the default.

@tcoopman
Copy link
Author

tcoopman commented Sep 7, 2015

So what's your opinion about this merge request.
Is this something that can be considered as a low level API with the current api on top?

Maybe my usecase for having access to the state in the actionCreators is not valid, but it looks reasonable to bind some state to the actoinCreators so that your components don't have to know about it.

@gaearon
Copy link
Contributor

gaearon commented Sep 13, 2015

I'm inclined not to merge this. This is a very bad case perf-wise (re-binding on every state change) that we won't ever encourage. It also makes API asymmetric and harder to remember.

If you want to do this, you can always introduce an intermediate component and call bindActionCreators from it in render() method, as it will have comparable performance.

@gaearon gaearon closed this Sep 13, 2015
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

Successfully merging this pull request may close these issues.

None yet

2 participants