Created
June 21, 2019 04:54
-
-
Save paulshen/eeed25d7dfa91e47df44a40cde592612 to your computer and use it in GitHub Desktop.
useMemo using useRef
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
function useMemo(f, deps) { | |
const state = React.useRef(); | |
if (state.current != null) { | |
const [prevDeps, prevResult] = state.current; | |
if (areArraysShallowEqual(deps, prevDeps)) { | |
return prevResult; | |
} | |
} | |
const result = f(); | |
state.current = [deps, result]; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment