Courses / React 19: From Components to Production / Chapter 8
Module 4 · Advanced Hooks and Concurrency · Chapter 8 of 30
Advanced Hooks: Refs, Reducers, Context, Layout Effects and Custom Hooks
Builds on State and Effects: useState, Events, useEffect and Cleanup and New in React 19: Actions, use, Optimistic UI and ref as a Prop. Next up: Concurrent Rendering: Transitions, Deferred Values and Priorities.
Part 1 · useRef: values that do not render and handles to DOM nodes
A box that survives renders
A mandi price board has to remember small things that never appear on screen, such as a running timer or the input that holds the cursor. State is the wrong place for these, because each state change asks React to render again.
A ref is the tool for values that must survive between renders without causing one.
useRef(initial) returns an object with one property, current, set to the initial value. React hands you the same object on every render. You can read and write current whenever you like, and React does not notice. That is the whole difference from state: writing to a ref does not schedule a render, and the value is not a snapshot, so a read always returns what was last written.
| State | Ref | |
|---|---|---|
| Changing it | calls a setter, which requests a render | assigns to current, nothing else happens |