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

Can you pass an argument to a selector? #38

Closed
0x80 opened this issue Aug 29, 2015 · 5 comments
Closed

Can you pass an argument to a selector? #38

0x80 opened this issue Aug 29, 2015 · 5 comments

Comments

@0x80
Copy link

0x80 commented Aug 29, 2015

I have a component that computes derived data from a store, but based on a variable argument. Can I use reselect for this?

For example I would like to move the following method to a selector. It merges the options and current values from a filter store based on the group argument:

getOptionsWithValues (group) {
    const { options, values } = this.props.filter
    const opts = options.filter(opt => opt.group === group)
    return opts.map(opt => ({ ...opt, value: values.get(opt.id) }))
  }

I feel like I should move this computation outside of the component. In a traditional Flux implementation I would create this method as a getter on the store but I don't know how to approach this with Redux.

@0x80
Copy link
Author

0x80 commented Aug 29, 2015

I just realized that "group" in this case should also be part of the application state. In which case all required data will be available to the selector. Hurray \o/

@0x80 0x80 closed this as completed Aug 29, 2015
@mindjuice
Copy link

That is generally my conclusion as well. See this issue for an additional take: #18

@0x80
Copy link
Author

0x80 commented Aug 29, 2015

👍

@niksosf
Copy link

niksosf commented Mar 3, 2017

I am not too sure about this, consider a search query like 'cheese' search against a shopping list, while getListItems gives you the same data, but as you search 'cheese', then 'bread', then 'cheese' again, the first and third searches won't be a cache hit as the the states representing them are different, even though they should return the same items

@flushentitypacket
Copy link

flushentitypacket commented Apr 29, 2019

@niksosf I agree, IMO this isn't sufficient explanation for certain use cases. However, I'd like to push back on your example, since user input for searches have infinite possibility, which is something that you might not want to always cache.

Maybe a practical example is an Admin Dashboard where you can select a particular User and display attributes about the user as a sortable chart.

In this case, you want to be able to select the User's data (which is the parameter to the selector) and cache the calculation (sortable chart).

I found this comment by the author to create a memoized factory for your selectors:
#18 (comment)

However, in my experience so far, this is a common enough use case that I wish there were more direct support for this in reselect.

EDIT: There is also an example of memoized factories in the README FAQs:
https://github.com/reduxjs/reselect/tree/ac77610bbb0a3cab9b280ea5ea379c2387017446#q-how-do-i-create-a-selector-that-takes-an-argument

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

No branches or pull requests

4 participants