DEV Community

Cover image for memo() in React . . .
_Khojiakbar_
_Khojiakbar_

Posted on

memo() in React . . .

Imagine you’re running a donut shop, and you’ve got a bunch of customers ordering the same donuts over and over. Every time someone orders a donut, you don't want to bake a new one if you already have a fresh one sitting on the counter. That would be a waste of time and ingredients, right?

In React, when you build an app, sometimes you have components that don't need to change unless their ingredients (props) change. This is where memo comes in handy. It’s like a magic wrapper that tells React, "Hey, if the ingredients (props) haven’t changed, just give me the same donut (component) that’s already on the counter. No need to bake a new one!"

How memo Works in Code

Let’s say we have a Donut component:

import React from 'react';

function Donut({ flavor }) {
  console.log(`Baking a ${flavor} donut...`);
  return <div>{flavor} donut</div>;
}

export default memo(Donut);
Enter fullscreen mode Exit fullscreen mode

Here’s what’s happening:

1. Donut Shop: The Donut component is like a baker who makes donuts based on the flavor you give them.

2. Order: When you call the Donut component with a flavor, it bakes a donut and logs "Baking a [flavor] donut...".

3. memo: The memo wrapper is like a wise manager who says, "Hey, if the customer wants the same flavor again, just give them the one we already made."

Fun Sample

Let's make a little app where you can order donuts:

import React, { useState } from 'react';
import Donut from './Donut'; // Our donut component

function App() {
  const [flavor, setFlavor] = useState('chocolate');
  const [count, setCount] = useState(0);

  return (
    <div>
      <h1>Donut Shop</h1>
      <Donut flavor={flavor} />
      <button onClick={() => setFlavor('chocolate')}>Order Chocolate</button>
      <button onClick={() => setFlavor('strawberry')}>Order Strawberry</button>
      <button onClick={() => setCount(count + 1)}>Click Counter {count}</button>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

What’s Happening Here?

Donut Component: The Donut component is baked only when the flavor changes.

Buttons:

  • Clicking "Order Chocolate" will order a chocolate donut.
  • Clicking "Order Strawberry" will order a strawberry donut.
  • Clicking "Click Counter" will increase a counter, but it doesn’t change the flavor.

memo in Action

If you click "Order Chocolate" over and over, _memo notices that the flavor isn’t changing and won’t log "Baking a chocolate donut..." again. But if you switch to "Order Strawberry", it will log "Baking a strawberry donut..."._

Why is This Cool?

memo saves React from doing unnecessary work. Just like in a real donut shop, you don’t want to bake a new donut if you already have one on the counter. It keeps things efficient and quick, just like giving customers their donuts faster!

Top comments (1)

Collapse
 
xjiaxiong profile image
jiaxiong

thank you