React Memoization with Objects and Arrays

A small React question 👇 //child component const Child = React.memo(({ name }) => { console.log("Child rendered"); return <div>{name}</div>; }); Parent component renders this: <Child name="Vivek" /> If the parent re-renders, will the child render again? ✅ No. Because React checks: "Vivek" === "Vivek" So React.memo skips the render. → Now a tiny change: <Child config={{ theme: "dark" }} /> Same question. Will the child render again? 👀 ➡️ Yes. Even though the object looks identical. Because in JavaScript: { theme: "dark" } !== { theme: "dark" } A new object is created on every render, so React thinks the prop changed. The fix is simple: const config = useMemo(() => ({ theme: "dark" }), []); <Child config={config} /> Now the reference stays the same and React.memo can actually skip the render. One of the biggest React performance lessons: Strings and numbers are usually safe. Objects, arrays, and functions can quietly break memoization. So if you want to avoid unnecessary re-renders in child components, be careful when passing objects as props. #react #javascript #frontend #webdevelopment #fullstackdevelopment #softwaredeveloper #fullstackdeveloper

To view or add a comment, sign in

Explore content categories