DEV Community

Jayant Khandelwal
Jayant Khandelwal

Posted on

1

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

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

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay