Current section: Custom Hooks 36 exercises
Problem

useCallback

Loading exercise

Transcript

00:00 So, what we've got works out pretty well for our specific use case, but once you make a reusable hook, you want to think about use cases that you haven't thought about yet. Things that people will do with your hook that you didn't intend on or plan on initially,

00:16 and it's perfectly reasonable for somebody to use your setSearchParams function in a use effect. And if they do, they need to provide that as part of the dependency array, because if they don't, then they could be calling setSearchParams with an old version of that function, which

00:34 can definitely cause problems. So we always want to include this in our dependency array, but the problem is that now setSearchParams is going to be different every single time a re-render happens, and so we're going to be calling this use effect every single time a re-render happens, which we may not actually want.

00:52 So, and of course, some condition needs to be in there also. Don't forget your dependencies, folks. So your job is to make it so that by including setSearchParams, we're not triggering the use effect unnecessarily. It should only be triggered when some condition changes. setSearchParams should be stable.

01:10 You're going to stabilize it with useCallback. All right, that should be enough for you to get going on with, so let's get going on.