Hooks 11 exercises
Problem

Typing the useMemo Hook

In this exercise, we're going to explore useMemo to understand what it's doing.

Here we have a useMemo function that takes in a function returning an array and a dependency array, similar to what we've seen with useEffect and useCallback:


export const Component = () => {
co

Loading exercise

Transcript

0:00 In this exercise, we're going to look at useMemo to try to understand what it's doing. We have useMemo here, and what it takes is it takes in a function that returns something. Then it basically takes in a dependency array that you can pass in, like we've seen with useEffect and useCallback.

0:16 Here, we're generating 100 random string UIDs. We don't want to rerun that every time the component runs, so we've stuck it in a useMemo to make sure that it only does it once when the component initiates.

0:28 The thing that we're getting back is not this array of strings here. We're getting back a function that returns an array of strings. Your job is to figure out what has gone wrong here and dive into the type signature of useMemo to figure out what the real API is type-wise.