Skip to content

Instantly share code, notes, and snippets.

@bryzettler
Created September 18, 2017 20:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryzettler/306b053c2028def8615d20f88b90494d to your computer and use it in GitHub Desktop.
Save bryzettler/306b053c2028def8615d20f88b90494d to your computer and use it in GitHub Desktop.
First Arg Reselect
/* eslint-disable import/prefer-default-export */
// @flow
import { createSelectorCreator } from 'reselect';
const defaultEqualityCheck = (a, b) => (a === b);
/**
* A selector that will only change the output if the first argument has changed.
*/
export const firstArgSelector = createSelectorCreator(
(func, equalityCheck = defaultEqualityCheck) => {
let lastArg = null;
let lastResult = null;
return (...args) => {
if (
lastArg !== null &&
equalityCheck(lastArg, args[0])
) {
return lastResult;
}
lastArg = args[0];
lastResult = func(...args);
return lastResult;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment