reselect icon indicating copy to clipboard operation
reselect copied to clipboard

Feature request: prop values selector

Open tgriesser opened this issue 11 years ago • 12 comments

A common pattern I find when composing selectors is to do something like this:

var myItem = createSelector(
  itemOne,
  itemTwo,
  itemThree,
  (itemOne, itemTwo, itemThree) => ({itemOne, itemTwo, itemThree})
)

It'd be a little cleaner if we could do:

var myItem = createSelector({itemOne, itemTwo, itemThree})

Where reselect would recognize the first arg is an object, pick the values off and spread those into the arguments, and then remap the result values of these functions to the keys.

var myItem = createSelector({
  keyOne: selectorForSome('value'),
  keyTwo
})
myItem(state) === {keyOne: value, keyTwo: value}

If this seems outside the scope of the lib, I can write it as a plugin, just thought I'd bring it up as a common pattern I've found useful.

tgriesser avatar Sep 03 '15 18:09 tgriesser

Interesting. I think I quite like this. It would be backwards compatible, so no problems there. I know some people like the explicitness of the library as it currently is, but in that case they could just ignore this feature. I would be interested to hear the thoughts of others.

ellbee avatar Sep 03 '15 20:09 ellbee

Curious, how would it be backwards incompatible?

tgriesser avatar Sep 03 '15 20:09 tgriesser

Ha, sorry I meant it won't be backwards incompatible!

ellbee avatar Sep 03 '15 20:09 ellbee

Ah gotcha! :)

tgriesser avatar Sep 03 '15 20:09 tgriesser

BTW, been using this quite a bit and it's pretty great, would love to see it added :)

tgriesser avatar Sep 09 '15 20:09 tgriesser

Cool, glad to hear its working for you. Now I have got 1.0.0 released this is next on the list.

@speedskater @faassen Any objections to this?

ellbee avatar Sep 09 '15 21:09 ellbee

@ellbee @tgriesser I like the idea but I would suggest creating explicit explicit method for this. We have already such a function. It is called composeSelectors, works on top of reselect and exactly has your suggested behaviour. I think we should add it as a feature. @PSpSynedra already implemented it together with tests. What to you think about it?

speedskater avatar Sep 10 '15 08:09 speedskater

Having it as a seperate function adds really nice syntax for composeability and some kind of named parameters. like this:

var itemSum = createSelector(composeSelectors({item1, item2, item3}), items => items.item1 + items.item2 + items.item3);

speedskater avatar Sep 10 '15 08:09 speedskater

@speedskater @PSpSynedra That sounds good to me. Do you want to make a PR?

ellbee avatar Sep 10 '15 20:09 ellbee

@ellbee Yes, will create it tomorrow.

PSpSynedra avatar Sep 10 '15 20:09 PSpSynedra

@PSpSynedra Awesome, thank you!

ellbee avatar Sep 10 '15 20:09 ellbee

This functionality has been added to Reselect v1.1.0 with the new function createStructuredSelector

ellbee avatar Sep 16 '15 16:09 ellbee