Skip to content

Instantly share code, notes, and snippets.

@paulshen
Created June 21, 2019 04:54
Show Gist options
  • Save paulshen/eeed25d7dfa91e47df44a40cde592612 to your computer and use it in GitHub Desktop.
Save paulshen/eeed25d7dfa91e47df44a40cde592612 to your computer and use it in GitHub Desktop.
useMemo using useRef
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