reselect icon indicating copy to clipboard operation
reselect copied to clipboard

Selector parameter from React Router

Open gerhardsletten opened this issue 10 years ago • 3 comments

Hi, I try to use create a selector with the param from React Router for a Master > Detail application, but it looks like it recalculate it every I go into the Detail-view. Should not this be cached?

(when I return to the Masterview, the id-param of the router will be undefined, but it should equal when I re-enter the Detail-view (and viewing an item with the same ID)

import {createSelector} from 'reselect'

const getRouterID = (state) => state.router.params.id
const getItems = (state) => state.items // array

export const getSelectedBook = createSelector(
  [getRouterID, getItems],
  (id, items) => {
    return items.filter((item) => {
      return item.id === id
  }
)

gerhardsletten avatar Feb 15 '16 21:02 gerhardsletten

Hi @gerhardsletten, that should work fine. If you can provide a runnable example of this not working I'll take a look.

ellbee avatar Feb 16 '16 20:02 ellbee

It works, but should not the items.filter... be cached?

In this application I have this list of items, and when I first enter an item it is runned, I then returned back to the list, but when I reenter the same item (same id) the filter-code is runned again. This is where I thought reselect should cache the returned content..

gerhardsletten avatar Feb 16 '16 21:02 gerhardsletten

Yes, we meant the same thing, sorry if I wasn't clear. By "it should work" I meant it should cache when the arguments haven't changed.

Have you checked this question from the FAQ?

https://github.com/reactjs/reselect#q-why-is-my-selector-recomputing-when-the-input-state-stays-the-same

I will need to see an example where Reselect is not behaving as you expect if I am going to be able to help further.

ellbee avatar Feb 16 '16 22:02 ellbee