Last active
January 28, 2022 17:19
-
-
Save obrenoco/6cdfd3959c8fa8460b2ce404df8f65a9 to your computer and use it in GitHub Desktop.
Rebuild useCallback and useMemo hooks from React
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
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