"React useMemo Challenge: Guess the Output"

Can You Guess the Output? (Challenge #9 – The useMemo Trap) Sometimes optimization in React creates more bugs than it fixes 😅 Here’s a real-world example 👇 function Child({ config }) { React.useEffect(() => { console.log("Effect ran"); }, [config]); return <p>{config.theme}</p>; } export default function App() { const [count, setCount] = React.useState(0); const config = React.useMemo(() => ({ theme: "dark" }), []); return ( <div> <Child config={config} /> <button onClick={() => setCount(count + 1)}>+</button> </div> ); } 🧩 Question: You click the “+” button 3 times. 👉 How many times does "Effect ran" appear in the console? And why? (Hint: think about dependency identity and memoization behavior) 💬 Drop your answer + reasoning below 👇Let’s see who really understands how useMemo and React’s dependency array work ⚙️ #React #JavaScript #Nextjs #Frontend #TypeScript #useMemo #Hooks #CleanCode #Performance #DeveloperCommunity #InterviewPreparation #WebDevelopment

the effect runs only once upon the initial mount because of the same object reference in the dependency array

Like
Reply

To view or add a comment, sign in

Explore content categories