Created
April 30, 2020 10:05
-
-
Save js2me/2e509d6f9a4060960d1bc0607793bbb2 to your computer and use it in GitHub Desktop.
createMemoContainer
This file contains hidden or 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
/** | |
* | |
* @param initialValue {T} initial value | |
* @param callback {function} used to update value | |
*/ | |
export const createMemoContainer = <T, ExtraArgs extends unknown[] = []>( | |
initialValue: T, | |
callback: (currentValue: T, ...args: ExtraArgs) => T | null, | |
): ((...args: ExtraArgs) => T) => { | |
let currentValue: T = initialValue; | |
return (...args: ExtraArgs): T => { | |
const newValue = callback(currentValue, ...args); | |
if (newValue === null) return currentValue; | |
currentValue = newValue; | |
return newValue; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment