From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

useCallback summary and pitfalls

useCallback summary and pitfalls

Welcome back. Let's discuss some common pitfalls using the use callback hook. Number one is missing dependencies in the array. It can lead to stale closures. So we have gone through this so many times now. So we have to be understood on this one. Like the other hooks, we should not be overusing the useCallback hook on trivial functions, such as having some small computations and where the re-render is actually not that big. For example, if you are just re-rendering a span or a p tag inside your child component, so even though that is going to re-render, that is not going to be much of an issue. where your child component is having a heavy JSX and that is getting re-rendered every time on the screen. So in that particular case, you should be considering using the use callback hook. The most important point, always profile first before doing any optimization. Based on the result of the profiling, apply your fixes. And we should not be thinking that it improves the performance always. At…

Contents