The Inline Style Anti-pattern
What you'll learn: Why passing inline style objects like style={{ margin: 10 }} silently breaks React's memoization optimizations.
The Problem with Inline Styles
You've likely seen (or written) code like this:
<div style={{ margin: 10, padding: 5 }}>Content</div>
It looks innocent, but there's a hidden performance cost: every render creates a brand new object. Even though the values are identical, { margin: 10 } !== { margin: 10 } in JavaScript—they're different references.
When you wrap a component with React.memo to prevent unnecessary re-renders, it compares props using shallow equality. If you pass an inline style object, the comparison always fails because you're passing a fresh object each time.
const MemoChild = React.memo(Child);
function Parent() {
return <MemoChild style={{ margin: 10 }} />; // New object every render!
}
React.memo thinks the style prop changed, so it re-renders the child—defeating the entire purpose of memoization.
The Fix: Extract to Constants
Move the style object outside the component:
const CHILD_STYLE = { margin: 10 };
function Parent() {
return <MemoChild style={CHILD_STYLE} />; // Same reference every time
}
Now React.memo works correctly because the reference doesn't change between renders.
For dynamic styles, use useMemo:
const style = useMemo(() => ({ margin: dynamicValue }), [dynamicValue]);
Key Takeaway: Inline style objects (style={{ ... }}) create new references every render, breaking React.memo. Extract styles to constants or memoize them with useMemo to preserve referential equality.