reselect icon indicating copy to clipboard operation
reselect copied to clipboard

Date selector always recalculated although Date only changes every 15 mins

Open pke opened this issue 10 years ago • 3 comments

I have this interval selector:

export const getDateTime = minuteInterval => {
  const coeff = 1000 * 60 * minuteInterval
  const now = new Date()
  return new Date(Math.floor(now.getTime() / coeff) * coeff)
}

export const selectDateTime = () => getDateTime(15)

Other selectors using this as an input keep re-evaluating even in between those 15 minutes the DateTime selector actually changes. How does reselect detect a change? Does it use == or ===?

pke avatar Apr 28 '16 15:04 pke

The default equality check is ===

ellbee avatar Apr 28 '16 15:04 ellbee

Hmmm. That would explain it of course. getDateTime(15) == getDateTime(15) but getDateTime(15) !== getDateTime(15)

~~Any idea how to solve this kind of problem?~~ Maybe like this?:

export const selectDateTime = createSelectorCreator(
  defaultMomoize, 
  (a,b) => a.getTime() === b.getTime()
)

pke avatar Apr 28 '16 15:04 pke

This seems to do the trick. Is it ok?

const dateEqualSelector = createSelectorCreator(
  defaultMemoize,
  (a,b) => {
    return a.getTime() === b.getTime()
  }
)
const intervalSelector = () => getDateTime(15)

export const selectDateTime = dateEqualSelector(
  intervalSelector,
  (interval) => interval
)

pke avatar Apr 28 '16 15:04 pke