Pass state as argument to resultFunc.
My code has reducers that expose "getter" functions that understand the internal structure of their state. I'm using Immutable to store this state.
It's handy to have the getters as they can internalize certain knowledge about the state - for example, ensuring Map entries are always accessed via a Number() call (MyState.someMap.get(Number(id))), or throwing if an invalid key is requested.
These exported getter functions receive the store's root state, as they are typically called from a react-redux connect().
Therefore, I'd like to receive the state into my resultFunc, instead of (or in addition to) the mapped dependencies, so I can continue to use my getters. This would probably best implemented as an alternative to createSelectorCreator.
I can make this change, but wondered if anyone else had any thoughts or objections to this approach.
The objections I've thought of are that it prevents the current convenient selector composition (assuming only the state is passed to the result func), and it would be easy to accidentally make your selector depend on sections of the state that aren't included in your dependencies.
I include an example just in case my explanation wasn't clear.
// model.js
const Group = Record({ id: null, type: null })
const User = Record({ id: null, defaultGroupId: null })
// GroupStateReducer.js
export default function GroupState(domain, action) {
if (domain == undefined) domain = Map(); // indexed groups by Number(group.id)
// rest of reducer
}
export function getGroup(id, state) {
return state.GroupState.get(Number(id)) || throw new Error('Unknown group');
}
// UserStateReducer.js
export default function UserState(domain, action) {
if (domain == undefined) domain = Map(); // indexed users by Number(user.id)
// rest of reducer
}
export const getAllUsers = (state) => state.UserState;
export function getUser(id, state) {
return state.UserState.get(Number(id)) || throw new Error('Unknown user');
}
// selectors.js
// with reselect as it stands, would be:
export const getGroupUserTypesCurrent = createSelector(
(state) => state.UserState,
(state) => state.GroupState,
// good bye encapsulation
(userState, groupState) => userState.map((user) => user.id + " default group type " + groupState.get(Number(user.defaultGroupId)).type
)
// I'd like to replace with:
// "createStateReceivingSelector" - naming things... caching things... etc.
export const getGroupUserTypes = createStateReceivingSelector(
(state) => state.UserState,
(state) => state.GroupState,
(state) => getAllUsers(state).map((user) => user.id + " default group type " + getGroup(user.defaultGroupId, state).type
)
I have only had time to briefly look at this, but my initial objection to passing state into the result selector is memoization—every time anything in the state changes, even if it is entirely unrelated to the result function, it will cause the result function to recompute. If you are using react-redux, can you pass the following directly to the mapToProps function and get the same results without using Reselect at all?
(state) => getAllUsers(state).map((user) => user.id + " default group type " + getGroup(user.defaultGroupId, state).type
Let me know if I am misunderstanding.
No rush on this one.
I could calculate the result in the example directly, but it's simplified just to show the "getters" setup I have. In reality I'm doing more sophisticated grouping, mapping and cross-referencing between various entries in the state tree to produce just what I want in the UI, and which is fairly expensive - hence the need for memoization.
I envisage only storing the result of the dependencies functions as the memoize index - (i.e. the current behaviour) - which is why this introduces the risk of creating a selector that accidentally depends on a part of the tree not explicitly specified in the dependencies.
I've committed how I imagine the change will look in fe8eaf07f873c91572dae9c622b06fd4f9e178f3. Of course this would be a BC break so any real PR would introduce the new createStateReceivingSelector and createStateReceivingSelectorCreator
That's great, thanks for the extra info. I'm going away for a couple of days, but I'll definitely have a look at this at the start of next week.
I'm just taking a look at this, and this may be a result of simplification made for the example again, but unless I am mistaken this would work the same as the example:
export function getGroup(id, groupState) {
return groupState.get(Number(id)) || throw new Error('Unknown group');
}
export const getGroupUserTypes = createSelector(
state => state.UserState,
state => state.GroupState,
(userState, groupState) => userState.map(
user => user.id + " default group type " + getGroup(user.defaultGroupId, groupState).type
)
)
I hope it doesn't look like I am trying to avoid engaging with your actual request (although I guess I sort of am! :smile:) but I would like to try to avoid extending the surface area of the Reselect API if possible. A concrete example that can't be implemented without introducing createStateReceivingSelector would definitely help as motivation for adding it.
I am looking for a robust discussion on this one anyway... :)
I've got a mix of reselect'ed and non memoized state-to-props conversions. I use the getGroup() getter from both - so I pass top-level state to it from connect(mapState), in redux-thunk'ed action functions (the (...) => (dispatch, getState) => ... pattern), and now the memoized reselect helpers.
So far, the only places that "know" the GroupState reducer state lies under the GroupState key in the top level state are all contained the GroupState reducer file.
My main argument is really just encapsulation. I could export an "overloaded" function that takes the top level state or just the GroupState child, but that's a code smell to me.
It's really a question of how much my state mapping functions should know about the shape of the underlying state. The getter functions have turned out to be very helpful when refactoring the shape - as an example, if I switched from numeric IDs to GUIDs, then I would only need change the getGroup helper.
I suppose I'm arguing for two different ways to extract things from the state - some that are tightly bound to the underlying state (my "getters") and some that just use properties of the "gotten" results.
Guess this is more of a fundamental question in redux rather than this fine library. I'll keep pondering it...
Thanks, it took me a while but I think I get it now!
So far, the only places that "know" the GroupState reducer state lies under the GroupState key in the top level state are all contained the GroupState reducer file.
In the example state.GroupState is referenced in selectors.js too. I guess you would export a getAllGroups function like getAllUsers. And if that is the case could you do this in selectors.js:
export const getGroupUserTypes = createSelector(
getAllUsers,
getAllGroups,
(users, groups) => users.map((user) => user.id + " default group type " + getGroup(user.defaultGroupId, groups).type
)
and something like this in the action without loss of encapsulation?
const state = getState()
getGroup(id, getAllGroups(state))
If anyone reading this has strong opinions about this either way, I'd love to hear them.
Fair point that state.GroupState is also referenced in the "proposed" selector. That's there to guard against, say, the type of a group changing - i.e. it just provides a dependency for checking. Adding a getAllGroups "getter" there would be preferable to diving into the state tree directly.
I am reluctant (heh) to make the getGroup(id, getAllGroups(state)) function. This is because it changes the "type signature" of an exported getter - eg getAllGroups(TopLevelState state) and getGroup(int index, GroupLevelState groupState) - assuming the TopLevelState was something like TopLevelState { GroupLevelState groups; UserLevelState users; }.
In my current pattern, my reducers end up looking something like, with java-ey type annotations,
class UserState { // whatever };
export default function (UserState domain, action) {
// the actual reducer
// returning new UserState by default
}
export function someKindOfGetter(TopLevelState state)
react-redux, redux-thunk, etc, generally pass the instance of TopLevelState, and all my current non-memoized prop building functions pass this into the getters.
This type signature of exported getters that take TopLevelState is consistent across all my getters to date, and that's really the part of encapsulation that I meant.
If you are OK with accessing the state tree directly from selectors.js:
export const getGroupUserTypes = createSelector(
createStructuredSelector({
UserState: state => state.UserState,
GroupState: state => state.GroupState
}),
state => getAllUsers(state).map((user) => user.id + " default group type " + getGroup(user.defaultGroupId, state).type
)
This approach would avoid the disadvantages you identified in the original comment.
The objections I've thought of are that it prevents the current convenient selector composition (assuming only the state is passed to the result func), and it would be easy to accidentally make your selector depend on sections of the state that aren't included in your dependencies.
Thanks for the idea and discussion, but I've decided that I'm not going to implement this in Reselect for the time being. I'd like to keep the API small and focused, and I don't personally share the concerns that these new functions mitigate. If I hear from others with similar concerns in the future then I'll look into it again.