Feature request: have `shouldRecompute` instead of custom equality function
See original GitHub issueI would like to be able to provide a function:
shouldRecompute: (lastResult: T, lastArgs: Array<unknown>, newArgs: Array<unknown>) => boolean
instead of custom equality function. In my case, whether the result should be recomputed depends on the previous result.
Use case:
I am building a calendar with monthly view. For input data (events etc.) and given date, I filter events and compute begin and end, which are first and last timestamp for that month. In shouldRecompute
, I would like just to check if new date is between these two values. Alternative is to have a custom equality which would compare months of previous and new date argument, but this is pretty costly (in general they are unix timestamps).
In general, my opinion is that shouldRecompute
is better than custom equality even for general case. Most of the time, arguments have different types, so custom equality ends up being a branchy if
statement checking for types of arguments and performing duty.
With shouldRecompute
, it would be possible to test all arguments at once, which is much easier to reason about.
E.g. before:
if (typeof a === 'string') {
return a === b;
} else if (a instanceof Date) {
return isSameMonth(a, b);
} else {
...
}
after:
return lastArgs[0] === newArgs[0] && isSameMonth(lastArgs[1], newArgs[1]) && ...;
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (7 by maintainers)
I like the latest API but still doesn’t work for my case. How about just pass
lastResult
as a third argument? That won’t break anything since previous arguments are unchanged.this seems like a reasonable path forward 👍