Created
September 18, 2017 20:35
-
-
Save bryzettler/306b053c2028def8615d20f88b90494d to your computer and use it in GitHub Desktop.
First Arg Reselect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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