DEV Community

loading...

Performance optimization Hook: *useCallback* and *useMemo* hook!

jackent2b profile image Jayant Khandelwal ・1 min read

What is useCallback hook?

useCallback hook is a hook that will return a memorized version of the callback function that only changes if one of the dependencies has changed.

Why do we use useCallback hook?

It is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders!

What is useMemo hook?

useMemo hook is very similar to useCallback hook but the difference is that useMemo hook invokes the provided function and caches its results.

useCallback vs useMemo

If you need to cache a function, then use useCallback hook whereas,
If you need to cache the result of the invoked function use useMemo hook.

Happy Coding!

-Jayant Khandelwal

Discussion (0)

pic
Editor guide