React Optimization: useMemo() hook.. Why first code in the image fails: In JavaScript, objects are compared by reference, not value. Even though { color: 'red' } looks the same as the previous render, React sees a new memory address. 👉 Result: The useEffect thinks options has changed and fires on every single render. You just created an infinite loop or an API spammer. Why the code below works: useMemo tells React: "Keep this object at the same memory address unless dependencies change." 👉 Result: The useEffect sees the exact same reference and skips the re-run. 💡 The Lesson: Don't just ask "Is this function heavy?" Also ask: "Am I passing this object/array into a dependency array?" If the answer is yes, you need useMemo to stabilize it. #ReactJS #WebDevelopment #JavaScript #CodingTips #FrontendEngineering
Just move the variable outside the component. Zero allocation cost. Zero hooks. Zero brain power. const OPTIONS = { color: 'red' } function Component() { useEffect(() => { api.getData(OPTIONS) }, []) // Safe. } And Module Scope != Global Scope and the React Compiler analyzes this and automatically caches the object literal (like _c[0] = { color: 'red' }).
Wrong! creating an object on rerenders take resources but how many? Memorizing takes resources as well but how many? Generallly speaking memo should only be used for heavy calculations and gaint object which have no place being in react to begin with. Do them outside. And btw it is in the React documentation…they specifically say - don’t use them unless it’s absolutely necessary. 99.9% of the time - it isn’t and hurts performance and build time. The example you provided falls into this as well
Brilliant breakdown! This is one of those patterns that separates performant React apps from ones that mysteriously slow down. The key insight is recognizing that dependency arrays don't just check values they check references. A new object literal every render = a new memory address = a dependency "change" even if the values are identical. One addition: while useMemo stabilizes the reference, don't over-memoize. Profile first to see which objects/arrays actually cause re-renders. The cost of computing a memoized value sometimes exceeds the cost of re-running the effect. The real win comes from asking: "Does this dependency array contain unstable references?" If yes → memoize. If no → skip it.
Sir / mam ಯಾರಾದ್ರೂ ವರ್ಕ್ ಹುಡ್ಕಡ್ತಿದೀರಾ Full - Time part time work from home ಎಲ್ಲ ರೀತಿಯಾಗಿ ವರ್ಕ್ ಮಾಡೋ ಅವಕಾಶ ಇದೆ ಯಾರಿಗೆ ಆಸಕ್ತಿ ಇದೆ ಅವರು ಕೆಳಗೆ ಕೊಟ್ಟಿರುವ ಅಪ್ಲಿಕೇಶನ್ ಅನ್ನು apply ಮಾಡಬಹುದು ....... ಅಪ್ಲಿಕೇಶನ್ ಅಕಿದ ನಂತರ ನಿಮಗೆ ಸಂಪೂರ್ಣ ಮಾಹಿತಿ ಕೊಡಲಾಗುತ್ತೆ....... ಧನ್ಯವಾದಗಳು 👍🏻👇🏻👇🏻 https://form.svhrt.com/68d161e14e60dbe1c6cb0f56
Nice example of how useMemo works. Alternatively, you could move the color variable to a constants object (or file) outside the component.
This is a great explanation of referential equality in React, Rafi Ahmed! It’s a common pitfall to assume that identical-looking objects are 'the same' across renders. Highlighting how useMemo() stabilizes memory addresses to prevent useEffect loops is a crucial lesson for anyone building performant apps.
This is something developers should never worry about. The library should have been optimal by default. Its not your job to make the library work properly