React.js • Chapter 21 • Foundations to Advanced
Memoization with useMemo
Study each React concept through explanations, focused examples, code, reasoning, expected behavior, practice, and review.
21.1 What useMemo Does
useMemo caches a calculated value between renders until one of its dependencies changes. In Chapter 21, the purpose is to make the behavior observable rather than memorizing an API. Follow the value from its source through the component tree and identify what React needs in order to produce the next interface. This section 21.1 connects the idea directly to Memoization with useMemo.
For What useMemo Does, inspect render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. A reliable React design makes ownership explicit, keeps rendering predictable, and separates calculations from synchronization with external systems. When a feature seems complicated, reduce it to one component, one state change, or one boundary and rebuild from that verified behavior. In Memoization with useMemo, keep the What useMemo Does responsibility visible while you test it.
This topic emphasizes structure. Use complete states for loading, success, empty data, and failure when those states can occur. After the example works, explain why React rendered what you see and which change would cause another render; that reasoning is more valuable than copying syntax. For section 21.1, relate that explanation back to What useMemo Does.
Key terms in plain language
- Rendering work — the calculations and DOM updates required to keep the interface synchronized.
- What — a focused part of what usememo does used to describe one responsibility, input, rendering decision, or boundary in the interface.
- useMemo — a focused part of what usememo does used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Does — a focused part of what usememo does used to describe one responsibility, input, rendering decision, or boundary in the interface.
10 teaching examples
Example 1: Two-component comparison
Build one version of the Account menu with the What useMemo Does responsibility in the parent and another with it in the child. Compare data ownership, reuse, and how many components need to know about the decision. useMemo caches a calculated value between renders until one of its dependencies changes.
Example 2: Failure or edge case
Create a safe edge case for What useMemo Does in the Data table: empty data, a missing prop, a rejected request, rapid clicks, or an unmounted element. Show the user a clear state instead of allowing confusing or stale output.
Example 3: Accessibility check
Use What useMemo Does in the Upload panel while testing keyboard access, semantic markup, labels, focus order, and understandable status feedback. React does not replace browser accessibility rules, so verify the generated interface.
Example 4: State ownership check
For the Team roster, identify which component truly owns the information involved in What useMemo Does. Remove duplicated state and derive values during rendering when they can be calculated from existing props or state. useMemo caches a calculated value between renders until one of its dependencies changes.
Example 5: Network-delay scenario
Assume the Analytics card is waiting on a slow API while using What useMemo Does. Decide what stays interactive, what shows pending feedback, what can be cancelled, and how stale responses are prevented from replacing newer data.
Example 6: Refactoring exercise
Take a large Booking flow component that mixes What useMemo Does with unrelated concerns. Extract one focused component or custom hook, give it a narrow API, and confirm that the user-visible behavior stays the same.
Example 7: Performance experiment
Profile the Support ticket before optimizing What useMemo Does. Record which components render, what calculation is costly, and whether the delay is actually noticeable; apply an optimization only when the measurement supports it. useMemo caches a calculated value between renders until one of its dependencies changes.
Example 8: Production review
Assume the Lesson tracker feature using What useMemo Does ships to many devices and languages. Review error recovery, loading states, accessibility, RTL layout, small-screen width, security boundaries, and whether monitoring can reveal failures.
Example 9: Smallest useful case
Create the smallest working version of What useMemo Does inside a Quiz screen. Keep one input and one visible result, then describe render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. This gives you a baseline before extra features hide the important behavior. useMemo caches a calculated value between renders until one of its dependencies changes.
Example 10: Change one input
Keep the Language selector example stable but change one input that affects What useMemo Does. Predict what React will render before running the code, then compare the result with your prediction and explain the render path.
React coding example
import { memo, useMemo, useState, useTransition } from 'react';
const Result = memo(function Result({ value }) {
return <p>Result: {value}</p>;
});
export default function TopicDemo() {
const [query, setQuery] = useState('');
const [isPending, startTransition] = useTransition();
const value = useMemo(() => query.trim().toUpperCase(), [query]);
return (
<section>
<input value={query} onChange={e => startTransition(() => setQuery(e.target.value))} />
<Result value={value} />
{isPending && <small>Updating…</small>}
</section>
);
}Step-by-step code explanation
- Identify the responsibility demonstrated by What useMemo Does.
- Read the component from inputs to returned JSX before focusing on individual syntax.
- Trace which event, prop, promise, or state update can cause the visible result to change.
- Test one normal path and one edge case so the behavior is not inferred from the happy path alone.
- Keep the example small enough that you can explain every render and every external side effect.
Expected behavior: A small React interface demonstrating What useMemo Does; the exact browser text depends on the interaction or data used in the example.
Practice exercise
Create a small interface focused on What useMemo Does. Write the expected screen state before running it, test one edge case, and change one input or interaction. Then explain which component owns the relevant data, what caused the render, and one accessibility or small-screen check you would perform before shipping the feature. Record the result as the Chapter 21 practice for Memoization with useMemo.
21.2 Memoizing Expensive Calculations
React memo can skip rendering a component when its props are considered equal. In Chapter 21, the purpose is to make the behavior observable rather than memorizing an API. Follow the value from its source through the component tree and identify what React needs in order to produce the next interface. This section 21.2 connects the idea directly to Memoization with useMemo.
For Memoizing Expensive Calculations, inspect render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. A reliable React design makes ownership explicit, keeps rendering predictable, and separates calculations from synchronization with external systems. When a feature seems complicated, reduce it to one component, one state change, or one boundary and rebuild from that verified behavior. In Memoization with useMemo, keep the Memoizing Expensive Calculations responsibility visible while you test it.
This topic emphasizes data flow. Use complete states for loading, success, empty data, and failure when those states can occur. After the example works, explain why React rendered what you see and which change would cause another render; that reasoning is more valuable than copying syntax. For section 21.2, relate that explanation back to Memoizing Expensive Calculations.
Key terms in plain language
- Rendering work — the calculations and DOM updates required to keep the interface synchronized.
- Memoizing — a focused part of memoizing expensive calculations used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Expensive — a focused part of memoizing expensive calculations used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Calculations — a focused part of memoizing expensive calculations used to describe one responsibility, input, rendering decision, or boundary in the interface.
10 teaching examples
Example 1: Failure or edge case
Create a safe edge case for Memoizing Expensive Calculations in the Analytics card: empty data, a missing prop, a rejected request, rapid clicks, or an unmounted element. Show the user a clear state instead of allowing confusing or stale output.
Example 2: Accessibility check
Use Memoizing Expensive Calculations in the Booking flow while testing keyboard access, semantic markup, labels, focus order, and understandable status feedback. React does not replace browser accessibility rules, so verify the generated interface.
Example 3: State ownership check
For the Support ticket, identify which component truly owns the information involved in Memoizing Expensive Calculations. Remove duplicated state and derive values during rendering when they can be calculated from existing props or state. React memo can skip rendering a component when its props are considered equal.
Example 4: Network-delay scenario
Assume the Lesson tracker is waiting on a slow API while using Memoizing Expensive Calculations. Decide what stays interactive, what shows pending feedback, what can be cancelled, and how stale responses are prevented from replacing newer data.
Example 5: Refactoring exercise
Take a large Course catalog component that mixes Memoizing Expensive Calculations with unrelated concerns. Extract one focused component or custom hook, give it a narrow API, and confirm that the user-visible behavior stays the same.
Example 6: Performance experiment
Profile the Profile settings before optimizing Memoizing Expensive Calculations. Record which components render, what calculation is costly, and whether the delay is actually noticeable; apply an optimization only when the measurement supports it. React memo can skip rendering a component when its props are considered equal.
Example 7: Production review
Assume the Search panel feature using Memoizing Expensive Calculations ships to many devices and languages. Review error recovery, loading states, accessibility, RTL layout, small-screen width, security boundaries, and whether monitoring can reveal failures.
Example 8: Smallest useful case
Create the smallest working version of Memoizing Expensive Calculations inside a Data table. Keep one input and one visible result, then describe render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. This gives you a baseline before extra features hide the important behavior. React memo can skip rendering a component when its props are considered equal.
Example 9: Change one input
Keep the Upload panel example stable but change one input that affects Memoizing Expensive Calculations. Predict what React will render before running the code, then compare the result with your prediction and explain the render path.
Example 10: Two-component comparison
Build one version of the Team roster with the Memoizing Expensive Calculations responsibility in the parent and another with it in the child. Compare data ownership, reuse, and how many components need to know about the decision. React memo can skip rendering a component when its props are considered equal.
React coding example
import { memo, useMemo, useState, useTransition } from 'react';
const Result = memo(function Result({ value }) {
return <p>Result: {value}</p>;
});
export default function TopicDemo() {
const [query, setQuery] = useState('');
const [isPending, startTransition] = useTransition();
const value = useMemo(() => query.trim().toUpperCase(), [query]);
return (
<section>
<input value={query} onChange={e => startTransition(() => setQuery(e.target.value))} />
<Result value={value} />
{isPending && <small>Updating…</small>}
</section>
);
}Step-by-step code explanation
- Identify the responsibility demonstrated by Memoizing Expensive Calculations.
- Read the component from inputs to returned JSX before focusing on individual syntax.
- Trace which event, prop, promise, or state update can cause the visible result to change.
- Test one normal path and one edge case so the behavior is not inferred from the happy path alone.
- Keep the example small enough that you can explain every render and every external side effect.
Expected behavior: A small React interface demonstrating Memoizing Expensive Calculations; the exact browser text depends on the interaction or data used in the example.
Practice exercise
Create a small interface focused on Memoizing Expensive Calculations. Write the expected screen state before running it, test one edge case, and change one input or interaction. Then explain which component owns the relevant data, what caused the render, and one accessibility or small-screen check you would perform before shipping the feature. Record the result as the Chapter 21 practice for Memoization with useMemo.
21.3 Dependencies and Recalculation
Dependencies and Recalculation is best understood through rendering work: the calculations and DOM updates required to keep the interface synchronized. Focus on measurement, scheduling, memoization, fallback UI, and user-perceived responsiveness. In Chapter 21, the purpose is to make the behavior observable rather than memorizing an API. Follow the value from its source through the component tree and identify what React needs in order to produce the next interface. This section 21.3 connects the idea directly to Memoization with useMemo.
For Dependencies and Recalculation, inspect inputs, component ownership, visible output, edge cases, and the reason another render occurs. A reliable React design makes ownership explicit, keeps rendering predictable, and separates calculations from synchronization with external systems. When a feature seems complicated, reduce it to one component, one state change, or one boundary and rebuild from that verified behavior. In Memoization with useMemo, keep the Dependencies and Recalculation responsibility visible while you test it.
This topic emphasizes edge cases. Use complete states for loading, success, empty data, and failure when those states can occur. After the example works, explain why React rendered what you see and which change would cause another render; that reasoning is more valuable than copying syntax. For section 21.3, relate that explanation back to Dependencies and Recalculation.
Key terms in plain language
- Rendering work — the calculations and DOM updates required to keep the interface synchronized.
- Dependencies — a focused part of dependencies and recalculation used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Recalculation — a focused part of dependencies and recalculation used to describe one responsibility, input, rendering decision, or boundary in the interface.
10 teaching examples
Example 1: Accessibility check
Use Dependencies and Recalculation in the Course catalog while testing keyboard access, semantic markup, labels, focus order, and understandable status feedback. React does not replace browser accessibility rules, so verify the generated interface.
Example 2: State ownership check
For the Profile settings, identify which component truly owns the information involved in Dependencies and Recalculation. Remove duplicated state and derive values during rendering when they can be calculated from existing props or state. Dependencies and Recalculation is best understood through rendering work: the calculations and DOM updates required to keep the interface synchronized. Focus on measurement, scheduling, memoization, fallback UI, and user-perceived responsiveness.
Example 3: Network-delay scenario
Assume the Search panel is waiting on a slow API while using Dependencies and Recalculation. Decide what stays interactive, what shows pending feedback, what can be cancelled, and how stale responses are prevented from replacing newer data.
Example 4: Refactoring exercise
Take a large Shopping cart component that mixes Dependencies and Recalculation with unrelated concerns. Extract one focused component or custom hook, give it a narrow API, and confirm that the user-visible behavior stays the same.
Example 5: Performance experiment
Profile the Dashboard filter before optimizing Dependencies and Recalculation. Record which components render, what calculation is costly, and whether the delay is actually noticeable; apply an optimization only when the measurement supports it. Dependencies and Recalculation is best understood through rendering work: the calculations and DOM updates required to keep the interface synchronized. Focus on measurement, scheduling, memoization, fallback UI, and user-perceived responsiveness.
Example 6: Production review
Assume the Message composer feature using Dependencies and Recalculation ships to many devices and languages. Review error recovery, loading states, accessibility, RTL layout, small-screen width, security boundaries, and whether monitoring can reveal failures.
Example 7: Smallest useful case
Create the smallest working version of Dependencies and Recalculation inside a Analytics card. Keep one input and one visible result, then describe inputs, component ownership, visible output, edge cases, and the reason another render occurs. This gives you a baseline before extra features hide the important behavior. Dependencies and Recalculation is best understood through rendering work: the calculations and DOM updates required to keep the interface synchronized. Focus on measurement, scheduling, memoization, fallback UI, and user-perceived responsiveness.
Example 8: Change one input
Keep the Booking flow example stable but change one input that affects Dependencies and Recalculation. Predict what React will render before running the code, then compare the result with your prediction and explain the render path.
Example 9: Two-component comparison
Build one version of the Support ticket with the Dependencies and Recalculation responsibility in the parent and another with it in the child. Compare data ownership, reuse, and how many components need to know about the decision. Dependencies and Recalculation is best understood through rendering work: the calculations and DOM updates required to keep the interface synchronized. Focus on measurement, scheduling, memoization, fallback UI, and user-perceived responsiveness.
Example 10: Failure or edge case
Create a safe edge case for Dependencies and Recalculation in the Lesson tracker: empty data, a missing prop, a rejected request, rapid clicks, or an unmounted element. Show the user a clear state instead of allowing confusing or stale output.
React coding example
import { memo, useMemo, useState, useTransition } from 'react';
const Result = memo(function Result({ value }) {
return <p>Result: {value}</p>;
});
export default function TopicDemo() {
const [query, setQuery] = useState('');
const [isPending, startTransition] = useTransition();
const value = useMemo(() => query.trim().toUpperCase(), [query]);
return (
<section>
<input value={query} onChange={e => startTransition(() => setQuery(e.target.value))} />
<Result value={value} />
{isPending && <small>Updating…</small>}
</section>
);
}Step-by-step code explanation
- Identify the responsibility demonstrated by Dependencies and Recalculation.
- Read the component from inputs to returned JSX before focusing on individual syntax.
- Trace which event, prop, promise, or state update can cause the visible result to change.
- Test one normal path and one edge case so the behavior is not inferred from the happy path alone.
- Keep the example small enough that you can explain every render and every external side effect.
Expected behavior: A small React interface demonstrating Dependencies and Recalculation; the exact browser text depends on the interaction or data used in the example.
Practice exercise
Create a small interface focused on Dependencies and Recalculation. Write the expected screen state before running it, test one edge case, and change one input or interaction. Then explain which component owns the relevant data, what caused the render, and one accessibility or small-screen check you would perform before shipping the feature. Record the result as the Chapter 21 practice for Memoization with useMemo.
21.4 When useMemo Does Not Help
useMemo caches a calculated value between renders until one of its dependencies changes. In Chapter 21, the purpose is to make the behavior observable rather than memorizing an API. Follow the value from its source through the component tree and identify what React needs in order to produce the next interface. This section 21.4 connects the idea directly to Memoization with useMemo.
For When useMemo Does Not Help, inspect render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. A reliable React design makes ownership explicit, keeps rendering predictable, and separates calculations from synchronization with external systems. When a feature seems complicated, reduce it to one component, one state change, or one boundary and rebuild from that verified behavior. In Memoization with useMemo, keep the When useMemo Does Not Help responsibility visible while you test it.
This topic emphasizes accessibility. Use complete states for loading, success, empty data, and failure when those states can occur. After the example works, explain why React rendered what you see and which change would cause another render; that reasoning is more valuable than copying syntax. For section 21.4, relate that explanation back to When useMemo Does Not Help.
Key terms in plain language
- Rendering work — the calculations and DOM updates required to keep the interface synchronized.
- When — a focused part of when usememo does not help used to describe one responsibility, input, rendering decision, or boundary in the interface.
- useMemo — a focused part of when usememo does not help used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Does — a focused part of when usememo does not help used to describe one responsibility, input, rendering decision, or boundary in the interface.
10 teaching examples
Example 1: State ownership check
For the Dashboard filter, identify which component truly owns the information involved in When useMemo Does Not Help. Remove duplicated state and derive values during rendering when they can be calculated from existing props or state. useMemo caches a calculated value between renders until one of its dependencies changes.
Example 2: Network-delay scenario
Assume the Message composer is waiting on a slow API while using When useMemo Does Not Help. Decide what stays interactive, what shows pending feedback, what can be cancelled, and how stale responses are prevented from replacing newer data.
Example 3: Refactoring exercise
Take a large Appointment form component that mixes When useMemo Does Not Help with unrelated concerns. Extract one focused component or custom hook, give it a narrow API, and confirm that the user-visible behavior stays the same.
Example 4: Performance experiment
Profile the Photo gallery before optimizing When useMemo Does Not Help. Record which components render, what calculation is costly, and whether the delay is actually noticeable; apply an optimization only when the measurement supports it. useMemo caches a calculated value between renders until one of its dependencies changes.
Example 5: Production review
Assume the Task board feature using When useMemo Does Not Help ships to many devices and languages. Review error recovery, loading states, accessibility, RTL layout, small-screen width, security boundaries, and whether monitoring can reveal failures.
Example 6: Smallest useful case
Create the smallest working version of When useMemo Does Not Help inside a Lesson tracker. Keep one input and one visible result, then describe render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. This gives you a baseline before extra features hide the important behavior. useMemo caches a calculated value between renders until one of its dependencies changes.
Example 7: Change one input
Keep the Course catalog example stable but change one input that affects When useMemo Does Not Help. Predict what React will render before running the code, then compare the result with your prediction and explain the render path.
Example 8: Two-component comparison
Build one version of the Profile settings with the When useMemo Does Not Help responsibility in the parent and another with it in the child. Compare data ownership, reuse, and how many components need to know about the decision. useMemo caches a calculated value between renders until one of its dependencies changes.
Example 9: Failure or edge case
Create a safe edge case for When useMemo Does Not Help in the Search panel: empty data, a missing prop, a rejected request, rapid clicks, or an unmounted element. Show the user a clear state instead of allowing confusing or stale output.
Example 10: Accessibility check
Use When useMemo Does Not Help in the Shopping cart while testing keyboard access, semantic markup, labels, focus order, and understandable status feedback. React does not replace browser accessibility rules, so verify the generated interface.
React coding example
import { memo, useMemo, useState, useTransition } from 'react';
const Result = memo(function Result({ value }) {
return <p>Result: {value}</p>;
});
export default function TopicDemo() {
const [query, setQuery] = useState('');
const [isPending, startTransition] = useTransition();
const value = useMemo(() => query.trim().toUpperCase(), [query]);
return (
<section>
<input value={query} onChange={e => startTransition(() => setQuery(e.target.value))} />
<Result value={value} />
{isPending && <small>Updating…</small>}
</section>
);
}Step-by-step code explanation
- Identify the responsibility demonstrated by When useMemo Does Not Help.
- Read the component from inputs to returned JSX before focusing on individual syntax.
- Trace which event, prop, promise, or state update can cause the visible result to change.
- Test one normal path and one edge case so the behavior is not inferred from the happy path alone.
- Keep the example small enough that you can explain every render and every external side effect.
Expected behavior: A small React interface demonstrating When useMemo Does Not Help; the exact browser text depends on the interaction or data used in the example.
Practice exercise
Create a small interface focused on When useMemo Does Not Help. Write the expected screen state before running it, test one edge case, and change one input or interaction. Then explain which component owns the relevant data, what caused the render, and one accessibility or small-screen check you would perform before shipping the feature. Record the result as the Chapter 21 practice for Memoization with useMemo.
21.5 Measuring Before Memoizing
React memo can skip rendering a component when its props are considered equal. In Chapter 21, the purpose is to make the behavior observable rather than memorizing an API. Follow the value from its source through the component tree and identify what React needs in order to produce the next interface. This section 21.5 connects the idea directly to Memoization with useMemo.
For Measuring Before Memoizing, inspect render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. A reliable React design makes ownership explicit, keeps rendering predictable, and separates calculations from synchronization with external systems. When a feature seems complicated, reduce it to one component, one state change, or one boundary and rebuild from that verified behavior. In Memoization with useMemo, keep the Measuring Before Memoizing responsibility visible while you test it.
This topic emphasizes production behavior. Use complete states for loading, success, empty data, and failure when those states can occur. After the example works, explain why React rendered what you see and which change would cause another render; that reasoning is more valuable than copying syntax. For section 21.5, relate that explanation back to Measuring Before Memoizing.
Key terms in plain language
- Rendering work — the calculations and DOM updates required to keep the interface synchronized.
- Measuring — a focused part of measuring before memoizing used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Before — a focused part of measuring before memoizing used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Memoizing — a focused part of measuring before memoizing used to describe one responsibility, input, rendering decision, or boundary in the interface.
10 teaching examples
Example 1: Network-delay scenario
Assume the Task board is waiting on a slow API while using Measuring Before Memoizing. Decide what stays interactive, what shows pending feedback, what can be cancelled, and how stale responses are prevented from replacing newer data.
Example 2: Refactoring exercise
Take a large Notification center component that mixes Measuring Before Memoizing with unrelated concerns. Extract one focused component or custom hook, give it a narrow API, and confirm that the user-visible behavior stays the same.
Example 3: Performance experiment
Profile the Quiz screen before optimizing Measuring Before Memoizing. Record which components render, what calculation is costly, and whether the delay is actually noticeable; apply an optimization only when the measurement supports it. React memo can skip rendering a component when its props are considered equal.
Example 4: Production review
Assume the Language selector feature using Measuring Before Memoizing ships to many devices and languages. Review error recovery, loading states, accessibility, RTL layout, small-screen width, security boundaries, and whether monitoring can reveal failures.
Example 5: Smallest useful case
Create the smallest working version of Measuring Before Memoizing inside a Search panel. Keep one input and one visible result, then describe render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. This gives you a baseline before extra features hide the important behavior. React memo can skip rendering a component when its props are considered equal.
Example 6: Change one input
Keep the Shopping cart example stable but change one input that affects Measuring Before Memoizing. Predict what React will render before running the code, then compare the result with your prediction and explain the render path.
Example 7: Two-component comparison
Build one version of the Dashboard filter with the Measuring Before Memoizing responsibility in the parent and another with it in the child. Compare data ownership, reuse, and how many components need to know about the decision. React memo can skip rendering a component when its props are considered equal.
Example 8: Failure or edge case
Create a safe edge case for Measuring Before Memoizing in the Message composer: empty data, a missing prop, a rejected request, rapid clicks, or an unmounted element. Show the user a clear state instead of allowing confusing or stale output.
Example 9: Accessibility check
Use Measuring Before Memoizing in the Appointment form while testing keyboard access, semantic markup, labels, focus order, and understandable status feedback. React does not replace browser accessibility rules, so verify the generated interface.
Example 10: State ownership check
For the Photo gallery, identify which component truly owns the information involved in Measuring Before Memoizing. Remove duplicated state and derive values during rendering when they can be calculated from existing props or state. React memo can skip rendering a component when its props are considered equal.
React coding example
import { memo, useMemo, useState, useTransition } from 'react';
const Result = memo(function Result({ value }) {
return <p>Result: {value}</p>;
});
export default function TopicDemo() {
const [query, setQuery] = useState('');
const [isPending, startTransition] = useTransition();
const value = useMemo(() => query.trim().toUpperCase(), [query]);
return (
<section>
<input value={query} onChange={e => startTransition(() => setQuery(e.target.value))} />
<Result value={value} />
{isPending && <small>Updating…</small>}
</section>
);
}Step-by-step code explanation
- Identify the responsibility demonstrated by Measuring Before Memoizing.
- Read the component from inputs to returned JSX before focusing on individual syntax.
- Trace which event, prop, promise, or state update can cause the visible result to change.
- Test one normal path and one edge case so the behavior is not inferred from the happy path alone.
- Keep the example small enough that you can explain every render and every external side effect.
Expected behavior: A small React interface demonstrating Measuring Before Memoizing; the exact browser text depends on the interaction or data used in the example.
Practice exercise
Create a small interface focused on Measuring Before Memoizing. Write the expected screen state before running it, test one edge case, and change one input or interaction. Then explain which component owns the relevant data, what caused the render, and one accessibility or small-screen check you would perform before shipping the feature. Record the result as the Chapter 21 practice for Memoization with useMemo.
Chapter 21 review — 20 questions and answers
1. What problem does What useMemo Does help solve in this chapter?
Answer: useMemo caches a calculated value between renders until one of its dependencies changes.
2. What should you inspect when What useMemo Does does not behave as expected?
Answer: Check render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. Reduce the example until you can identify the input, render decision, update, and visible result.
3. How can you practice What useMemo Does without copying a large application?
Answer: Build a small component focused on What useMemo Does, predict its output, change one condition, and explain why React renders the new result.
4. What production concern belongs with What useMemo Does?
Answer: Review error recovery, accessibility, performance, security boundaries, localization, and small-screen behavior according to what What useMemo Does touches.
5. What problem does Memoizing Expensive Calculations help solve in this chapter?
Answer: React memo can skip rendering a component when its props are considered equal.
6. What should you inspect when Memoizing Expensive Calculations does not behave as expected?
Answer: Check render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. Reduce the example until you can identify the input, render decision, update, and visible result.
7. How can you practice Memoizing Expensive Calculations without copying a large application?
Answer: Build a small component focused on Memoizing Expensive Calculations, predict its output, change one condition, and explain why React renders the new result.
8. What production concern belongs with Memoizing Expensive Calculations?
Answer: Review error recovery, accessibility, performance, security boundaries, localization, and small-screen behavior according to what Memoizing Expensive Calculations touches.
9. What problem does Dependencies and Recalculation help solve in this chapter?
Answer: Dependencies and Recalculation is best understood through rendering work: the calculations and DOM updates required to keep the interface synchronized. Focus on measurement, scheduling, memoization, fallback UI, and user-perceived responsiveness.
10. What should you inspect when Dependencies and Recalculation does not behave as expected?
Answer: Check inputs, component ownership, visible output, edge cases, and the reason another render occurs. Reduce the example until you can identify the input, render decision, update, and visible result.
11. How can you practice Dependencies and Recalculation without copying a large application?
Answer: Build a small component focused on Dependencies and Recalculation, predict its output, change one condition, and explain why React renders the new result.
12. What production concern belongs with Dependencies and Recalculation?
Answer: Review error recovery, accessibility, performance, security boundaries, localization, and small-screen behavior according to what Dependencies and Recalculation touches.
13. What problem does When useMemo Does Not Help help solve in this chapter?
Answer: useMemo caches a calculated value between renders until one of its dependencies changes.
14. What should you inspect when When useMemo Does Not Help does not behave as expected?
Answer: Check render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. Reduce the example until you can identify the input, render decision, update, and visible result.
15. How can you practice When useMemo Does Not Help without copying a large application?
Answer: Build a small component focused on When useMemo Does Not Help, predict its output, change one condition, and explain why React renders the new result.
16. What production concern belongs with When useMemo Does Not Help?
Answer: Review error recovery, accessibility, performance, security boundaries, localization, and small-screen behavior according to what When useMemo Does Not Help touches.
17. What problem does Measuring Before Memoizing help solve in this chapter?
Answer: React memo can skip rendering a component when its props are considered equal.
18. What should you inspect when Measuring Before Memoizing does not behave as expected?
Answer: Check render frequency, prop identity, measured cost, scheduling, and user-visible responsiveness. Reduce the example until you can identify the input, render decision, update, and visible result.
19. How can you practice Measuring Before Memoizing without copying a large application?
Answer: Build a small component focused on Measuring Before Memoizing, predict its output, change one condition, and explain why React renders the new result.
20. What production concern belongs with Measuring Before Memoizing?
Answer: Review error recovery, accessibility, performance, security boundaries, localization, and small-screen behavior according to what Measuring Before Memoizing touches.