Hacker News new | ask | show | jobs
by sarahdellysse 1040 days 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])
1 comments

correct, 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.