Sub topic 3
Cheaper fixes: move state down and pass children
Assume you know function components, JSX, useState, props, children, and JavaScript reference values. Before adding memoization, reduce the part of the tree that must render.
Move state down
State updates re-render the component that owns the state and its descendants. If a search field updates state in a large page component, the whole page may render on every keystroke. Move that state into the smallest component that needs it.
import { useState } from 'react';
export function Page() {
return (
<>
<SearchBox />
<LargeContent />
</>
);
}
function SearchBox() {
const [query, setQuery] = useState('');
return <input value={query} onChange={(event) => setQuery(event.target.value)} />;
}
function LargeContent() {
return <section>Large content</section>;
}This approach often removes more work than wrapping LargeContent in memo.
Pass children through stable boundaries
A component with local state can receive expensive content as children. React can reuse the already-created children when the parent updates.
function Panel({ children }) {
const [open, setOpen] = useState(true);
return (
<>
<button onClick={() => setOpen((value) => !value)}>Toggle</button>
{open && children}
</>
);
}Use this composition pattern when the stateful component does not need to create or change the expensive subtree.
Context changes and memoized components
memo compares props, but it does not block updates caused by a context value read inside the component. A theme or user context change can re-render every consumer. Split the consumer from the expensive presentation component, and pass only the needed value as a prop. Also avoid putting frequently changing values and stable values in one context when consumers need only one of them.
Custom comparison risks
A custom memo comparison must check every prop that affects output, including functions. Returning true for an ignored function can leave a component with a stale closure and outdated behavior. The comparison itself also costs time. Prefer normal shallow comparison unless profiling shows a clear benefit.
When memoization does not pay off
Do not memoize automatically. It may add comparison work, memory use, and dependency-management complexity. It has little effect when props change every render, the component is cheap, or the parent rarely updates. Profile first, then try state placement or composition before memo, useMemo, or useCallback.
React Compiler
The React Compiler can automatically preserve values and skip compatible work in supported projects. It reduces the need for manual memoization, but it does not replace profiling or good state boundaries. Follow the compiler’s setup and directives when needed, and keep manual memoization for measured cases or code the compiler cannot optimize.
Exercise
A text input causes a large sibling tree to render. What should you try first?
Show answer
Move the input state into a smaller component so updates do not reach the large sibling tree.