Days of Better React Old Approach → Better Approach Faced a subtle performance issue recently. A child component was re-rendering even when props didn’t change. The reason? A function was being recreated on every render. ❌ Old approach: const handleClick = () => { console.log("Clicked"); }; <Child onClick={handleClick} /> This creates a new function on every render. ✅ Better approach: const handleClick = useCallback(() => { console.log("Clicked"); }, []); <Child onClick={handleClick} /> Now the function reference stays stable. Small optimization. Cleaner rendering behavior. Performance issues are often about references — not logic. #reactjs #frontenddeveloper #javascript #performance #webdevelopment #jamesCodeLab #fblifestyle
it's also necessary, I think, to keep in mind that this type of pattern is not necessary at all in some cases.
Brother there is a syntax error in your optimized code you should call the callback hook and must passed the dependency array
Great
This is an anti-pattern. In addition to syntactic bloat, now you have to manage dependency array to access outer scope variables. When a parent re-renders, the baseline expectation is that all children re-render. Fighting against this is going against the grain. This pattern is only acceptable in exceptional scenarios, typically complex or 3rd party components that have a lot of internal state.