Skip to content

Instantly share code, notes, and snippets.

@obrenoco
Last active January 28, 2022 17:19
Show Gist options
  • Save obrenoco/6cdfd3959c8fa8460b2ce404df8f65a9 to your computer and use it in GitHub Desktop.
Save obrenoco/6cdfd3959c8fa8460b2ce404df8f65a9 to your computer and use it in GitHub Desktop.
Rebuild useCallback and useMemo hooks from React
type CallbackProps = (...args: any[]) => any;
const useCallback2 = <T extends CallbackProps>(
y: T,
dependencies: unknown[]
): T => {
const [myState, setMyState] = useState<T>(() => y);
useEffect(() => {
setMyState(() => y);
}, dependencies);
return myState;
};
function useMemo2<T>(y: () => T, dependencies: unknown[]): T {
const [myState, setMyState] = useState<T>(y);
useEffect(() => {
setMyState(y);
}, dependencies);
return myState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment