If React receives the same element as last render, it skips re-rendering it.
This means reusing elements can significantly improve performance.
function Message({ greeting }) {console.log('rendering', greeting)return <div>{greeting}</div>}function Counter() {const [count, setCount] = useState(0)return (<><button onClick={() => setCount(c => c + 1)}>Count: {count}</button><Message greeting="Hello!" /></>)}
Even though greeting never changes, Message re-renders on every click.
const message = <Message greeting="Hello!" />function Counter() {const [count, setCount] = useState(0)return (<><button onClick={() => setCount(c => c + 1)}>Count: {count}</button>{message}</>)}
Now Message renders only once.
const footer = <Footer />function App() {const [count, setCount] = useState(0)return (<><button onClick={() => setCount(c => c + 1)}>Click</button>{footer}</>)}
Avoid re-rendering components that don’t depend on state.
useMemoWhen JSX depends on state:
const memoFooter = useMemo(() => <Footer name={name} />, [name])
Re-renders only when name changes.
React.memoconst Footer = memo(function Footer({ color, name }) {return <footer style={{ color }}>Hello {name}</footer>})