Cache Computed Values for Better Performance with the useMemo React Hook

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

If you have a React component that is computing a value that is expensive to compute, then you can cache that value with the useMemo hook, so that it's only re-computed when one of its dependencies change.

In this lesson, we'll use useMemo to memoize (cache) a label that is computed when the user presses a button - but we'll prevent it from being computed when the user updates other parts of the state (an input text value in this case).

Lecturer: [00:00] We have a React app. We can enter our name and press this button. It will show how many clicks we have. We do this by keeping track of the count and the name. We compute this label every time we render and display it down here.

[00:17] Let's import useMemo from React. We can use useMemo to cache this value. Let's say that our label now equals useMemo, and useMemo takes a function. The return value of this function should be this label that we're already computing.

[00:39] To see how many times this runs, let's put a console statement here. If we save and use this, and expand the console here, we can see it computed once when it reloads. It computes every time we press the button, but it also computes every single time we type a letter into this box.

[01:01] To prevent that, we can pass an array of dependencies to useMemo. This array of dependencies represents any values inside of this function that we need to compute the output.

[01:12] For us, that's just the count. We can save this. You can see it computes once, when the component reloads, and once every time we press the button. Now, if we type our name it doesn't compute again, because name is not included in these dependencies.

[01:29] We can keep pressing the button. It keeps computing once every time the count changes. If we didn't put anything in this array -- and save that -- this button would never work. This is computed once when the component loads, but it would never be computed again. We always want to keep all of our dependencies necessary in this array.

[01:49] Finally, we don't want to rely 100 percent on this function only being called when these dependencies change. The React docs say that this may be called more often for memory reasons, for example. But it is a way to suggest a performance improvement by only computing a value when certain dependencies change.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today