The purpose of this repository is to help learners understand when to use Memoization in React. Specifically how we can use the useMemo to optimize the performance of our components.
- clone and download all npm modules
- get an API key from emoji-api.com
- npm start
Environment Variables
REACT_APP_EMOJI_API_KEY=
The Problem: The main branch of this repository is set up to show how it can be really easy to code yourself into a corner in React, especially when using the React Hooks pattern.
- Inside of
App.jsyou'll find that there is a simple button that, when pressed, fetches emojis and displays them to the user. There is also a 'doubler' function that takes a number value from an input and doubles it output and prints it to the DOM. - If you look at the
consoleyou'll see that when clicking theGet Emojis <button>our logs inside the 'double function' get called as well. doubleis an expensive algorithm (not really but it's faking it for the example) and we don't want to run it every time we call the API at the press of a button.- This is what we aim to fix. We don't want to make any unnecessary calls to an API if we don't have to.
The Solution
- Wrapping
doublein auseMemohook and addingnumberto it's dependency array will solve our problem for us.
const doubledNumber = useMemo(() => {
return double(number);
}, [number]);- Be sure to import
useMemofrom react and be sure to return theoutput of doublefrom within your useMemo. - Now your logs should not log a call to
doublewhen you're fetching the emoji data.
