bindSelectorCreators to state
Hi. It would be useful to have bindSelectors function which can bind getState like
Something like https://github.com/reactjs/redux/blob/master/src/bindActionCreators.js
What do you think?
Can you give me some example code showing how this would be used? I guess you want access to the entire state within the selector?
Something like
const state = { filters: [...] };
function getState() {
return state;
}
const getFilter = createSelector(state => state.filters);
const boundSelectors = bindSelectors({
getFilter
}, getState);
getFilter(state); // -> [...]
boundSelectors.getFilter() // -> [...]
I just want to use selectors like bound actions. setFilter action, getFilter selector...
Where in the app structure would this likely be used? bindActionCreators generally gets used inside of mapDispatchToProps. I'm not immediately able to think of an equivalent place that would have access to getState like that. Just trying to visualize use cases.
I use angular and initialize everything in root directive.
angular.component('app', {
template: `<app-child></app-child>`,
controller: function (appReducer, appActions, appSelectors) {
var store = Redux.createStore(appReducer);
this.actions = Redux.bindActionCreators(appActions, store,dispatch);
this.selectors = Reselect.bindSelectors(appSelectors, store.getState);
}
});
angular.component('app-child', {
require: {
app: '^^app'
},
template: `<div></div>`,
controller: function () {
this.$onInit = function () {
// Something with it
this.app.actions.boundAction();
this.app.selectors.boundSelector();
};
}
});
I have custom realization of connect, but I still don't want to pass state to be consistent with actions.
Ok, I get it now, thanks for making the PR (#106). I don't think there is anything in the PR that depends on any other part of Reselect, so I wonder if it make sense to have it be its own module instead? I'd be interested in other peoples opinions on this.
Not every part should be dependent to be in one module. Moving out so specific helper doesn't seem like a good idea.
/cc @gaearon
Not every part should be dependent to be in one module.
No, and I didn't mean to imply it was. As it is possible to make this a separate module, I was trying to start a discussion about whether it was a good idea.
Moving out so specific helper doesn't seem like a good idea.
Ok, but why not? Is this helper really specific to Reselect? Could it be useful for normal, non-memoized selectors? Are there other libraries where this might be useful (e.g. https://github.com/tgriesser/simple-selectors)? Is the use case this PR addresses common enough to be worth increasing the surface area of the API by 50% for all users?
I could use something like this as well for my Redux app. We have some things in the component's state and not in the Redux store. Our use case is the user selecting a row in a DataGrid. Upon selection, we set a value in state indicating the currently selected item. When an item is selected, we would like to use reselect in order to de-normalize the entity and its child properties.
I think the idea should be similar to actionCreators. Maybe bindSelectorCreators to allow multiple instances.
@ellbee Okay. But simple-selectors copied part of functionality. Maintainer of it can copy again. But this functionality should be here without unnecessary splitting. Many redux features can be splitted in the same way, but it's wasting of modules.
But this functionality should be here without unnecessary splitting.
The API of Reselect is purposefully small. Adding to it is not done without careful consideration, as it is much easier to add something than remove it later.
In my opinion, here are two possible reasons for rejecting a proposed new feature:
- The feature is not useful for the vast majority of Reselect users.
I don't think the proposed feature will commonly be used in a React context. Not being an Angular user, I don't know if the pattern of initialising in the root directive is common. In your opinion, is this a "standard" way to hook up selectors in Angular?
(Also, as a side question are there any good Angular+Redux boilerplates/tutorials/discussions out there? I'd like to check them out.)
- The feature doesn't rely on any Reselect internals and so could be used with non-memoized selectors / other selector libraries.
In your example it looks to me like the proposed feature could also be used for non-memoized selectors. If so, then maybe it is a better fit for an Angular/Redux helper library (like ng-redux?).
Please correct me if you think I am wrong about this being a feature more useful for Angular users. Can you think of other common use cases for it?
@jsheetzati You seem to have a non-Angular use case, but I'm afraid I didn't really follow it. Can you show me some code?
Many redux features can be splitted in the same way, but it's wasting of modules.
Reselect was a feature proposed for Redux that was instead split out into its own module. I hope that we aren't a waste of a module! 😉
By the way, thanks for opening for the issue and creating the PR. It may seem like I am against adding this feature but I'm definitely not, I just want to make sure we properly justify its inclusion.
@ellbee Okay.
For me redux is a bundle of features. But reselect is a feature and I would like to have all related stuff in one context.
bindSelectorCreators idea is useful for only memoized selectors where we need to create a bunch of instances of selector via make* functions. And sharing selectors across multiple components becomes easier. So it's useful for react components too.
Okay. After some experience I see your point have more sense. Thanks for conversation.