3 ms·
> I’m still not exactly sure what the difference between useMemo and useCallback is const useCallback = (fn, deps) => useMemo(() => fn, deps); That's it.
by sarahdellysse 3y ago
> I’m still not exactly sure what the difference between useMemo and useCallback is
const useCallback = (fn, deps) => useMemo(() => fn, deps);
That's it. That's all it is. `useCallback` is a shorthand for `useMemo`, when you want to memo a function definition.
Example:
const handleButtonClick = useCallback((event) => {
event.preventDefault();
window.alert(`Hello, ${name}!`)
}, [name])
is just shorthand for
const handleButtonClick = useMemo(() => (event) => {
event.preventDefault();
window.alert(`Hello, ${name}!`)
}, [name])
- text0404 3y agocorrect, but with useCallback you don't take an additional perf hit for redefining the inner function on every render. useCallback is for callbacks, useMemo is for memoized data.