React.js • Chapter 16 • Foundations to Advanced
Refs with useRef
Study each React concept through explanations, focused examples, code, reasoning, expected behavior, practice, and review.
16.1 Persistent Mutable Values
Persistent Mutable Values is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization. In Chapter 16, 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 16.1 connects the idea directly to Refs with useRef.
For Persistent Mutable Values, 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 Refs with useRef, keep the Persistent Mutable Values 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 16.1, relate that explanation back to Persistent Mutable Values.
Key terms in plain language
- Hook — a React function that lets a component use state, context, refs, effects, or reusable logic.
- Persistent — a focused part of persistent mutable values used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Mutable — a focused part of persistent mutable values used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Values — a focused part of persistent mutable values used to describe one responsibility, input, rendering decision, or boundary in the interface.
10 teaching examples
Example 1: Refactoring exercise
Take a large Search panel component that mixes Persistent Mutable Values 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 2: Performance experiment
Profile the Shopping cart before optimizing Persistent Mutable Values. Record which components render, what calculation is costly, and whether the delay is actually noticeable; apply an optimization only when the measurement supports it. Persistent Mutable Values is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 3: Production review
Assume the Dashboard filter feature using Persistent Mutable Values 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 4: Smallest useful case
Create the smallest working version of Persistent Mutable Values inside a Team roster. 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. Persistent Mutable Values is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 5: Change one input
Keep the Analytics card example stable but change one input that affects Persistent Mutable Values. Predict what React will render before running the code, then compare the result with your prediction and explain the render path.
Example 6: Two-component comparison
Build one version of the Booking flow with the Persistent Mutable Values 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. Persistent Mutable Values is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 7: Failure or edge case
Create a safe edge case for Persistent Mutable Values in the Support ticket: 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 8: Accessibility check
Use Persistent Mutable Values in the Lesson tracker 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 9: State ownership check
For the Course catalog, identify which component truly owns the information involved in Persistent Mutable Values. Remove duplicated state and derive values during rendering when they can be calculated from existing props or state. Persistent Mutable Values is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 10: Network-delay scenario
Assume the Profile settings is waiting on a slow API while using Persistent Mutable Values. Decide what stays interactive, what shows pending feedback, what can be cancelled, and how stale responses are prevented from replacing newer data.
React coding example
import { useEffect, useRef, useState } from 'react';
export default function TopicDemo() {
const [status, setStatus] = useState('idle');
const targetRef = useRef(null);
useEffect(() => {
setStatus('synchronized');
return () => setStatus('idle');
}, []);
return <div ref={targetRef}>Topic: {"Persistent Mutable Values"} — {status}</div>;
}Step-by-step code explanation
- Identify the responsibility demonstrated by Persistent Mutable Values.
- 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 Persistent Mutable Values; the exact browser text depends on the interaction or data used in the example.
Practice exercise
Create a small interface focused on Persistent Mutable Values. 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 16 practice for Refs with useRef.
16.2 Reading DOM Nodes
Reading DOM Nodes is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization. In Chapter 16, 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 16.2 connects the idea directly to Refs with useRef.
For Reading DOM Nodes, inspect DOM ownership, ref lifetime, imperative actions, and avoiding render-time mutation. 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 Refs with useRef, keep the Reading DOM Nodes 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 16.2, relate that explanation back to Reading DOM Nodes.
Key terms in plain language
- Hook — a React function that lets a component use state, context, refs, effects, or reusable logic.
- Reading — a focused part of reading dom nodes used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Nodes — a focused part of reading dom nodes used to describe one responsibility, input, rendering decision, or boundary in the interface.
10 teaching examples
Example 1: Performance experiment
Profile the Appointment form before optimizing Reading DOM Nodes. Record which components render, what calculation is costly, and whether the delay is actually noticeable; apply an optimization only when the measurement supports it. Reading DOM Nodes is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 2: Production review
Assume the Photo gallery feature using Reading DOM Nodes 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 3: Smallest useful case
Create the smallest working version of Reading DOM Nodes inside a Support ticket. Keep one input and one visible result, then describe DOM ownership, ref lifetime, imperative actions, and avoiding render-time mutation. This gives you a baseline before extra features hide the important behavior. Reading DOM Nodes is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 4: Change one input
Keep the Lesson tracker example stable but change one input that affects Reading DOM Nodes. Predict what React will render before running the code, then compare the result with your prediction and explain the render path.
Example 5: Two-component comparison
Build one version of the Course catalog with the Reading DOM Nodes 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. Reading DOM Nodes is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 6: Failure or edge case
Create a safe edge case for Reading DOM Nodes in the Profile settings: 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 7: Accessibility check
Use Reading DOM Nodes in the Search 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 8: State ownership check
For the Shopping cart, identify which component truly owns the information involved in Reading DOM Nodes. Remove duplicated state and derive values during rendering when they can be calculated from existing props or state. Reading DOM Nodes is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 9: Network-delay scenario
Assume the Dashboard filter is waiting on a slow API while using Reading DOM Nodes. Decide what stays interactive, what shows pending feedback, what can be cancelled, and how stale responses are prevented from replacing newer data.
Example 10: Refactoring exercise
Take a large Message composer component that mixes Reading DOM Nodes 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.
React coding example
import { useEffect, useRef, useState } from 'react';
export default function TopicDemo() {
const [status, setStatus] = useState('idle');
const targetRef = useRef(null);
useEffect(() => {
setStatus('synchronized');
return () => setStatus('idle');
}, []);
return <div ref={targetRef}>Topic: {"Reading DOM Nodes"} — {status}</div>;
}Step-by-step code explanation
- Identify the responsibility demonstrated by Reading DOM Nodes.
- 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 Reading DOM Nodes; the exact browser text depends on the interaction or data used in the example.
Practice exercise
Create a small interface focused on Reading DOM Nodes. 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 16 practice for Refs with useRef.
16.3 Refs vs State
State is component memory that persists between renders. Updating state schedules another render rather than changing the current render in place. In Chapter 16, 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 16.3 connects the idea directly to Refs with useRef.
For Refs vs State, inspect initial state, update timing, immutable changes, and the next render. 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 Refs with useRef, keep the Refs vs State 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 16.3, relate that explanation back to Refs vs State.
Key terms in plain language
- Hook — a React function that lets a component use state, context, refs, effects, or reusable logic.
- Refs — a focused part of refs vs state used to describe one responsibility, input, rendering decision, or boundary in the interface.
- State — a focused part of refs vs state used to describe one responsibility, input, rendering decision, or boundary in the interface.
10 teaching examples
Example 1: Production review
Assume the Quiz screen feature using Refs vs State 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 2: Smallest useful case
Create the smallest working version of Refs vs State inside a Profile settings. Keep one input and one visible result, then describe initial state, update timing, immutable changes, and the next render. This gives you a baseline before extra features hide the important behavior. State is component memory that persists between renders. Updating state schedules another render rather than changing the current render in place.
Example 3: Change one input
Keep the Search panel example stable but change one input that affects Refs vs State. Predict what React will render before running the code, then compare the result with your prediction and explain the render path.
Example 4: Two-component comparison
Build one version of the Shopping cart with the Refs vs State 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. State is component memory that persists between renders. Updating state schedules another render rather than changing the current render in place.
Example 5: Failure or edge case
Create a safe edge case for Refs vs State in the Dashboard filter: 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 6: Accessibility check
Use Refs vs State in the Message composer 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 7: State ownership check
For the Appointment form, identify which component truly owns the information involved in Refs vs State. Remove duplicated state and derive values during rendering when they can be calculated from existing props or state. State is component memory that persists between renders. Updating state schedules another render rather than changing the current render in place.
Example 8: Network-delay scenario
Assume the Photo gallery is waiting on a slow API while using Refs vs State. Decide what stays interactive, what shows pending feedback, what can be cancelled, and how stale responses are prevented from replacing newer data.
Example 9: Refactoring exercise
Take a large Task board component that mixes Refs vs State 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 10: Performance experiment
Profile the Notification center before optimizing Refs vs State. Record which components render, what calculation is costly, and whether the delay is actually noticeable; apply an optimization only when the measurement supports it. State is component memory that persists between renders. Updating state schedules another render rather than changing the current render in place.
React coding example
import { useEffect, useRef, useState } from 'react';
export default function TopicDemo() {
const [status, setStatus] = useState('idle');
const targetRef = useRef(null);
useEffect(() => {
setStatus('synchronized');
return () => setStatus('idle');
}, []);
return <div ref={targetRef}>Topic: {"Refs vs State"} — {status}</div>;
}Step-by-step code explanation
- Identify the responsibility demonstrated by Refs vs State.
- 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 Refs vs State; the exact browser text depends on the interaction or data used in the example.
Practice exercise
Create a small interface focused on Refs vs State. 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 16 practice for Refs with useRef.
16.4 Avoiding Render-Time Ref Reads
Avoiding Render-Time Ref Reads is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization. In Chapter 16, 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 16.4 connects the idea directly to Refs with useRef.
For Avoiding Render-Time Ref Reads, inspect DOM ownership, ref lifetime, imperative actions, and avoiding render-time mutation. 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 Refs with useRef, keep the Avoiding Render-Time Ref Reads 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 16.4, relate that explanation back to Avoiding Render-Time Ref Reads.
Key terms in plain language
- Hook — a React function that lets a component use state, context, refs, effects, or reusable logic.
- Avoiding — a focused part of avoiding render-time ref reads used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Render-Time — a focused part of avoiding render-time ref reads used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Reads — a focused part of avoiding render-time ref reads used to describe one responsibility, input, rendering decision, or boundary in the interface.
10 teaching examples
Example 1: Smallest useful case
Create the smallest working version of Avoiding Render-Time Ref Reads inside a Dashboard filter. Keep one input and one visible result, then describe DOM ownership, ref lifetime, imperative actions, and avoiding render-time mutation. This gives you a baseline before extra features hide the important behavior. Avoiding Render-Time Ref Reads is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 2: Change one input
Keep the Message composer example stable but change one input that affects Avoiding Render-Time Ref Reads. Predict what React will render before running the code, then compare the result with your prediction and explain the render path.
Example 3: Two-component comparison
Build one version of the Appointment form with the Avoiding Render-Time Ref Reads 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. Avoiding Render-Time Ref Reads is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 4: Failure or edge case
Create a safe edge case for Avoiding Render-Time Ref Reads in the Photo gallery: 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 5: Accessibility check
Use Avoiding Render-Time Ref Reads in the Task board 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 6: State ownership check
For the Notification center, identify which component truly owns the information involved in Avoiding Render-Time Ref Reads. Remove duplicated state and derive values during rendering when they can be calculated from existing props or state. Avoiding Render-Time Ref Reads is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 7: Network-delay scenario
Assume the Quiz screen is waiting on a slow API while using Avoiding Render-Time Ref Reads. Decide what stays interactive, what shows pending feedback, what can be cancelled, and how stale responses are prevented from replacing newer data.
Example 8: Refactoring exercise
Take a large Language selector component that mixes Avoiding Render-Time Ref Reads 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 9: Performance experiment
Profile the Account menu before optimizing Avoiding Render-Time Ref Reads. Record which components render, what calculation is costly, and whether the delay is actually noticeable; apply an optimization only when the measurement supports it. Avoiding Render-Time Ref Reads is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 10: Production review
Assume the Data table feature using Avoiding Render-Time Ref Reads 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.
React coding example
import { useEffect, useRef, useState } from 'react';
export default function TopicDemo() {
const [status, setStatus] = useState('idle');
const targetRef = useRef(null);
useEffect(() => {
setStatus('synchronized');
return () => setStatus('idle');
}, []);
return <div ref={targetRef}>Topic: {"Avoiding Render-Time Ref Reads"} — {status}</div>;
}Step-by-step code explanation
- Identify the responsibility demonstrated by Avoiding Render-Time Ref Reads.
- 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 Avoiding Render-Time Ref Reads; the exact browser text depends on the interaction or data used in the example.
Practice exercise
Create a small interface focused on Avoiding Render-Time Ref Reads. 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 16 practice for Refs with useRef.
16.5 Common Ref Patterns
Common Ref Patterns is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization. In Chapter 16, 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 16.5 connects the idea directly to Refs with useRef.
For Common Ref Patterns, inspect DOM ownership, ref lifetime, imperative actions, and avoiding render-time mutation. 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 Refs with useRef, keep the Common Ref Patterns 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 16.5, relate that explanation back to Common Ref Patterns.
Key terms in plain language
- Hook — a React function that lets a component use state, context, refs, effects, or reusable logic.
- Common — a focused part of common ref patterns used to describe one responsibility, input, rendering decision, or boundary in the interface.
- Patterns — a focused part of common ref patterns used to describe one responsibility, input, rendering decision, or boundary in the interface.
10 teaching examples
Example 1: Change one input
Keep the Task board example stable but change one input that affects Common Ref Patterns. Predict what React will render before running the code, then compare the result with your prediction and explain the render path.
Example 2: Two-component comparison
Build one version of the Notification center with the Common Ref Patterns 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. Common Ref Patterns is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 3: Failure or edge case
Create a safe edge case for Common Ref Patterns in the Quiz screen: 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 4: Accessibility check
Use Common Ref Patterns in the Language selector 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 5: State ownership check
For the Account menu, identify which component truly owns the information involved in Common Ref Patterns. Remove duplicated state and derive values during rendering when they can be calculated from existing props or state. Common Ref Patterns is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 6: Network-delay scenario
Assume the Data table is waiting on a slow API while using Common Ref Patterns. Decide what stays interactive, what shows pending feedback, what can be cancelled, and how stale responses are prevented from replacing newer data.
Example 7: Refactoring exercise
Take a large Upload panel component that mixes Common Ref Patterns 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 8: Performance experiment
Profile the Team roster before optimizing Common Ref Patterns. Record which components render, what calculation is costly, and whether the delay is actually noticeable; apply an optimization only when the measurement supports it. Common Ref Patterns is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
Example 9: Production review
Assume the Analytics card feature using Common Ref Patterns 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 10: Smallest useful case
Create the smallest working version of Common Ref Patterns inside a Photo gallery. Keep one input and one visible result, then describe DOM ownership, ref lifetime, imperative actions, and avoiding render-time mutation. This gives you a baseline before extra features hide the important behavior. Common Ref Patterns is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
React coding example
import { useEffect, useRef, useState } from 'react';
export default function TopicDemo() {
const [status, setStatus] = useState('idle');
const targetRef = useRef(null);
useEffect(() => {
setStatus('synchronized');
return () => setStatus('idle');
}, []);
return <div ref={targetRef}>Topic: {"Common Ref Patterns"} — {status}</div>;
}Step-by-step code explanation
- Identify the responsibility demonstrated by Common Ref Patterns.
- 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 Common Ref Patterns; the exact browser text depends on the interaction or data used in the example.
Practice exercise
Create a small interface focused on Common Ref Patterns. 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 16 practice for Refs with useRef.
Chapter 16 review — 20 questions and answers
1. What problem does Persistent Mutable Values help solve in this chapter?
Answer: Persistent Mutable Values is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
2. What should you inspect when Persistent Mutable Values 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.
3. How can you practice Persistent Mutable Values without copying a large application?
Answer: Build a small component focused on Persistent Mutable Values, predict its output, change one condition, and explain why React renders the new result.
4. What production concern belongs with Persistent Mutable Values?
Answer: Review error recovery, accessibility, performance, security boundaries, localization, and small-screen behavior according to what Persistent Mutable Values touches.
5. What problem does Reading DOM Nodes help solve in this chapter?
Answer: Reading DOM Nodes is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
6. What should you inspect when Reading DOM Nodes does not behave as expected?
Answer: Check DOM ownership, ref lifetime, imperative actions, and avoiding render-time mutation. Reduce the example until you can identify the input, render decision, update, and visible result.
7. How can you practice Reading DOM Nodes without copying a large application?
Answer: Build a small component focused on Reading DOM Nodes, predict its output, change one condition, and explain why React renders the new result.
8. What production concern belongs with Reading DOM Nodes?
Answer: Review error recovery, accessibility, performance, security boundaries, localization, and small-screen behavior according to what Reading DOM Nodes touches.
9. What problem does Refs vs State help solve in this chapter?
Answer: State is component memory that persists between renders. Updating state schedules another render rather than changing the current render in place.
10. What should you inspect when Refs vs State does not behave as expected?
Answer: Check initial state, update timing, immutable changes, and the next render. Reduce the example until you can identify the input, render decision, update, and visible result.
11. How can you practice Refs vs State without copying a large application?
Answer: Build a small component focused on Refs vs State, predict its output, change one condition, and explain why React renders the new result.
12. What production concern belongs with Refs vs State?
Answer: Review error recovery, accessibility, performance, security boundaries, localization, and small-screen behavior according to what Refs vs State touches.
13. What problem does Avoiding Render-Time Ref Reads help solve in this chapter?
Answer: Avoiding Render-Time Ref Reads is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
14. What should you inspect when Avoiding Render-Time Ref Reads does not behave as expected?
Answer: Check DOM ownership, ref lifetime, imperative actions, and avoiding render-time mutation. Reduce the example until you can identify the input, render decision, update, and visible result.
15. How can you practice Avoiding Render-Time Ref Reads without copying a large application?
Answer: Build a small component focused on Avoiding Render-Time Ref Reads, predict its output, change one condition, and explain why React renders the new result.
16. What production concern belongs with Avoiding Render-Time Ref Reads?
Answer: Review error recovery, accessibility, performance, security boundaries, localization, and small-screen behavior according to what Avoiding Render-Time Ref Reads touches.
17. What problem does Common Ref Patterns help solve in this chapter?
Answer: Common Ref Patterns is best understood through hook: a React function that lets a component use state, context, refs, effects, or reusable logic. Focus on dependency choices, cleanup, state ownership, and synchronization.
18. What should you inspect when Common Ref Patterns does not behave as expected?
Answer: Check DOM ownership, ref lifetime, imperative actions, and avoiding render-time mutation. Reduce the example until you can identify the input, render decision, update, and visible result.
19. How can you practice Common Ref Patterns without copying a large application?
Answer: Build a small component focused on Common Ref Patterns, predict its output, change one condition, and explain why React renders the new result.
20. What production concern belongs with Common Ref Patterns?
Answer: Review error recovery, accessibility, performance, security boundaries, localization, and small-screen behavior according to what Common Ref Patterns touches.