Skip to main content

React.memo perform shallow comparsion of objects ie non-primitive data

 

  1. React.memo performs a shallow comparison, so nested objects are not compared by their contents.

    It compares the references of object props using Object.is().

    Example

    jsx
    const Child = React.memo(({ user }) => {
      console.log("Child rendered");
      return <p>{user.profile.name}</p>;
    });

    Suppose the parent passes this object:

    jsx
    const user = {
      name: "John",
      profile: {
        name: "John Doe"
      }
    };

    Case 1: New outer object, same nested values

    js
    const prevUser = {
      name: "John",
      profile: { name: "John Doe" }
    };
    
    const nextUser = {
      name: "John",
      profile: { name: "John Doe" }
    };

    React compares the user prop references:

    js
    Object.is(prevUser, nextUser); // false

    Result: The component re-renders because the outer object reference changed. React doesn't need to compare the nested properties.

    Case 2: Same outer object, nested object changed

    js
    const user = {
      name: "John",
      profile: { name: "John Doe" }
    };
    
    const prevUser = user;
    
    user.profile.name = "Jane Doe";
    
    const nextUser = user;
    
    Object.is(prevUser, nextUser); // true

    Result: React.memo considers the user prop unchanged, because the outer object reference is the same.

    Even though the nested value changed, the shallow comparison doesn't detect it.

    Note that mutating objects this way is generally discouraged in React because it can cause UI updates to be missed.

    Remember this rule

    • Shallow comparison: Compares the outer prop values or references.

    • Deep comparison: Recursively compares nested object contents.

    • React.memo: Uses shallow prop comparison by default, not deep comparison.

    In short, React.memo checks whether the user object is the same object, not whether every property inside it is the same.

Comments

Popular posts from this blog

Ternary operator and its working on mechanism on array

  If condition is true → use valueIfTrue If condition is false → use valueIfFalse ✅ Your amenity example Your code: {amenity === 'wifi' ? 'WIFI' : amenity} Breakdown: ✔ Condition: amenity === 'wifi' This checks: “Is the current amenity equal to 'wifi'?” ✔ If condition is TRUE → show: 'WIFI' ✔ If condition is FALSE → show: amenity 🎯 Example with your amenitiesList const amenitiesList = [ 'wifi' , 'parking' , 'kitchen' ]; Looping through each item: 🔹 1. amenity = "wifi" Check condition: 'wifi' === 'wifi' → TRUE So ternary returns: 'WIFI' 👉 Display: WIFI 🔹 2. amenity = "parking" Check condition: 'parking' === 'wifi' → FALSE So ternary returns: amenity // which is "parking" 👉 Display: parking 🔹 3. amenity = "kitchen" Check condition: 'kitchen' === 'wifi' → FALS...

Tailwind css and its corresponding vanilla css

Tailwind Class CSS Property Vanilla CSS Equivalent Description bg-white background-color background-color: #ffffff; Sets background color to white. shadow-sm box-shadow box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05); Adds a small shadow under the element. border-b border-bottom-width border-bottom-width: 1px; Adds a bottom border. Default color: #e5e7eb (gray-200). max-w-7xl max-width max-width: 80rem; /* 1280px */ Restricts container width for large screens. mx-auto margin-left , margin-right margin-left: auto; margin-right: auto; Centers the container horizontally. px-4 padding-left , padding-right padding-left: 1rem; padding-right: 1rem; Adds horizontal padding (16px). sm:px-6 Responsive padding (small screens ≥640px) @media (min-width: 640px) { padding-left: 1.5rem; padding-right: 1.5rem; } Increases padding on small screens. lg:px-8 Responsive padding (large screens ≥1024px) @media (min-width: 1024px) { padding-left: 2rem; paddi...

Role of box-sizing and its attributes in css

  🧱 Default behavior (content-box) By default, browsers use: box-sizing : content-box; This means: Total element width = content width + padding + border So if you have: .container { width : 300px ; padding : 20px ; border : 5px solid black; } Then the total visible width becomes: 300 (content) + 40 ( left + right padding) + 10 ( left + right border) = 350 px ⚠️ The box becomes wider than 300px , which can cause overflow or layout shifts. ✅ With box-sizing: border-box When you use: box-sizing : border-box; the formula changes to: Total element width = width (including padding + border) So the same CSS now behaves like this: width = 300 px (total) → content area = 300 - ( 40 padding + 10 border) = 250 px ✅ The box stays exactly 300px wide — no overflow. 🎯 Why it’s useful Prevents unexpected overflow due to padding/borders Makes responsive layouts easier Keeps your box sizes consistent You can trust width to be the actu...