When we initially set up our new React project and decided on using Re-Select to memoize our selectors, a pattern was chosen to create wrapper functions around the calls to createSelector() in order to potentially parameterize some of the memoization based on separate component context...
However, over time this has been the exception rather than the rule, and now some of the devs on our team are having a back-and-forth about whether making multiple calls to createSelector with the same inputs results in multiple separate memoized copies of the resulting data, or if they all point to the same result.
Example:
const _getFoos = (state) => state.foos;
const _mapFoosToItems = (foos) => foos.map(createFooItem);
export const selectFooItems = () => {
return createSelector(_getFoos, _mapFoosToItems);
}
// Component1.tsx
const mapStateToProps = {
fooItems: selectFooItems()
}
// Component2.tsx
const mapStateToProps = {
fooItems: selectFooItems()
}
In the above example, are we creating 2 completely separate, memoized selectors with duplicate but separate memoized results? Or does createSelector somehow automagically know that these two selectors are the same because the input selector functions and outputs are the same?
In the example above, since we have no context specific parameters, would it be better to remove the anonymous wrapper around createSelector and just set selectFooItems directly to the createSelector result
Example:
export const selectFooitems = createSelector(_getFoos, _mapFoosToItems);
// Component1.tsx
const mapStateToProps = {
fooItems: selectFooItems
}
// Component2.tsx
const mapStateToProps = {
fooItems: selectFooItems
}