Skip to the chapter
←ReactGGroundwork⚛React
React/Beginner/★
57 / 57 written chapters
/

The cover
  1. B1Setup & JSX
  2. B2Thinking in React
  3. B3Components
  4. B4Props
  5. B5useState
  6. B6Events & conditionals
  7. B7Lists & keys
  8. B8Forms
  9. B9useEffect
  10. B10Lifting state & styling
  11. B11Guided project
  12. ★Cheat page
  1. I1useRef
  2. I2Context
  3. I3useReducer
  4. I4Custom hooks
  5. I5Effects in depth
  6. I6Routing
  7. I7State architecture
  8. I8Memoisation
  9. I9Boundaries & portals
  10. I10Data fetching
  11. I11Forms at scale
  12. I12State libraries
  13. I13Auth
  14. I14TanStack Query
  15. I15Testing
  16. I16TypeScript
  17. I17Animation
  18. I18E2E testing
  19. I19i18n
  20. I20File uploads
  21. I21Real-time UI
  22. I22shadcn/ui
  23. ★Cheat page
  1. A1Fiber
  2. A2Concurrent
  3. A3Suspense
  4. A4Server Components
  5. A5Server Actions
  6. A6use()
  7. A7Rendering strategies
  8. A8Effect timing
  9. A9External stores
  10. A10Refs in depth
  11. A11Component patterns
  12. A12Performance
  13. A13Accessibility
  14. A14Design systems
  15. A15Advanced TypeScript
  16. A16Architecture at scale
  17. A17Custom renderers
  18. A18Migrating & upgrading
  19. A19Production debugging
  20. A20Review & judgement
  21. A21State machines
  22. A22Feature flags
  23. A23Interview bank
  24. ★Cheat page

⚛★ · Part 1 · Beginner

The React cheat page

Everything worth having in front of you, on one page.

4 min read11 sections2 exercises
57 of 57

In this chapter

  • Hooks, at a glance
  • The rules of hooks, and why
  • Rendering — the 30-second version
  • Keys
  • State: replace, never mutate

and 6 more

about 4 min
11On this page
  1. Hooks, at a glance
  2. The rules of hooks, and why
  3. Rendering — the 30-second version
  4. Keys
  5. State: replace, never mutate
  6. Which state goes where
  7. Effects — the taxonomy
  8. The bugs, and their one-line fixes
  9. Server and client
  10. Performance, in order
  11. The accessibility minimum

Hooks, at a glance

HookUse it forWatch for
useStateValue the UI rendersUpdater form when the next value depends on the current
useReducerSeveral fields that move togetherdispatch is stable — never needs a dependency
useRefDOM nodes; values that survive but do not renderNever read or write during render
useEffectSynchronising with something outside ReactMost effects should not exist
useLayoutEffectMeasure and adjust before paintBlocks painting; warns during SSR
useEffectEventLatest value inside an effect, without depending on itOnly callable from an effect in the same component
useMemoExpensive value; referential stabilityCosts more than it saves for arithmetic
useCallbackStable function identityPointless unless the consumer is memoised or a dependency
useTransitionMark an update interruptibleGives isPending; dim, do not blank
useDeferredValueSame, when the value is a propRenders twice by design
useIdLinking label to input in a reusable componentNot for list keys
useSyncExternalStoreState outside ReactSnapshot must be referentially stable
useContextReading ambient valuesEvery consumer re-renders on identity change
useImperativeHandleExposing a small API instead of a nodeUsually a prop would do

The rules of hooks, and why

  • Top level only. React matches hooks to state by call order, so a hook in a condition shifts every slot after it.
  • From React functions only. Components or other hooks — there is no instance to attach to otherwise.
  • The exception: use() can be called conditionally, because it stores nothing in the hook list.

Rendering — the 30-second version

state changes → render (pure, interruptible) → diff → commit (sync) → paint → effects
  • Each render is a snapshot. count does not change mid-render.
  • Setting state schedules, it does not assign. Three setCount(count + 1) add one.
  • Diffing is per type and per position. Different type at a position → unmount and remount, state lost.
  • Children re-render by default when a parent does. Usually fine.

Keys

What a key isItem identity between renders — not a hint, not a perf trick
Index keys break whenThe list reorders, or an item is removed from anywhere but the end
What breaksState, focus, scroll and inputs are preserved on the wrong row
Deliberate usekey={userId} to reset a component's state on purpose
UniqueAmong siblings only, not globally

State: replace, never mutate

[...items, item]                                   // add
items.filter((i) => i.id !== id)                    // remove
items.map((i) => (i.id === id ? { ...i, done: true } : i))   // update one
[...items].sort()                                   // sort — copy first
{ ...user, address: { ...user.address, city } }     // nested — spread each level

Mutating gives React the same reference back, so it concludes nothing changed and skips the render.

Which state goes where

KindHome
From a serverA query cache — TanStack Query, or the framework's loader
Should survive refresh or a shared linkThe URL
One componentuseState, right there
A few nearby componentsLift to the closest common parent
Everywhere, changes rarelyContext
Everywhere, changes oftenA store with selectors
Does not affect renderingA ref

Effects — the taxonomy

You want to…Use
Compute from props or stateNothing. Calculate during render.
React to a click or submitThe event handler
Reset state when a prop changesA key
Cache an expensive calculationuseMemo
Subscribe to an external storeuseSyncExternalStore
Fetch dataA data library, or the server
Connect a socket, timer or browser APIan effect

The bugs, and their one-line fixes

SymptomCauseFix
A stray 0 on the page{items.length && ...}length > 0 &&
Infinite render loopEffect sets state, depends on an object made during renderDepend on a primitive
Counter stuck at 1Stale closure in an intervalUpdater form, or useEffectEvent
Typed text jumps rowsIndex keysStable ids
Input is read-onlyvalue with no onChangeAdd the handler
"changed uncontrolled to controlled"useState() with no argumentInitialise to ""
Wrong data after fast navigationResponse raceAbortController in the cleanup
Child loses state every renderComponent defined inside a componentMove it to module scope
memo does nothingAn inline arrow or object propuseCallback / useMemo, or move state down
Page reloads on submitNo preventDefaultAdd it, on the <form>

Server and client

Server Component   no state, no effects, no handlers · can await · ships 0 JS
"use client"       a doorway, not a label — everything it imports is client too
props across       must serialise: no functions (except Server Actions)
"use server"       a public HTTP endpoint — authenticate, authorise, validate

Performance, in order

  1. Profile with "why did this render" on. Do not guess.
  2. Move state down, or pass the expensive subtree as children.
  3. Virtualise past a few hundred rows.
  4. Code-split by route; find the one dependency that is a third of the bundle.
  5. Then memoise, exactly where the profiler pointed.

The accessibility minimum

  • A real <button>, not a div with onClick.
  • Every control has an accessible name; a placeholder is not a label.
  • Move focus on route change — client navigation does not reset it.
  • Do not disable the submit button; explain the failure instead.
  • Tab through the flow with the mouse away. Nothing else catches what that catches.

02Practice this chapter

Opens in the editor: write it, run it, and check it against real tests.

  • 8 testsDiagnose it from the symptomintermediate
  • 8 testsWhich state goes wherebeginner
All 46 for this level →
★

Finished reading?

0 of 57 chapters read in this series

→Next stop

  1. A23Previous · A23React interview bank
  2. ★you are here
  3. The endBack to the coverAll chapters

01On this page

11 sections · 0%

  1. 01Hooks, at a glance
  2. 02The rules of hooks, and why
  3. 03Rendering — the 30-second version
  4. 04Keys
  5. 05State: replace, never mutate
  6. 06Which state goes where
  7. 07Effects — the taxonomy
  8. 08The bugs, and their one-line fixes
  9. 09Server and client
  10. 10Performance, in order
  11. 11The accessibility minimum

[] chapters · t top