React · Chapter 18 of 26
Refs
By the end of this chapter you can keep a value between renders that does not cause a render (useRef), reach a real element on the page to focus or scroll it, pass a ref to your own component as a prop, keep an interval's or a timeout's id where the next click can find it, and say when a value belongs in a ref and when in state. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Effects and data
- 4 tasks
- React
- In the all labs plan
The first 2 chapters of React are free. This one comes with the all labs plan.
Why you need this
Ira's designs have three things that React's descriptions cannot do on their own. Pressing / anywhere on Parda's home page should put the cursor in the search box, and the box should have it already when the page opens. On a phone, row F of Kala Talkies is wider than the screen, so when you pick F20 the row should slide until F20 is in the middle. Both need the real element on the page: focus() and scrollIntoView() are methods of a DOM element, and your component only ever returns descriptions of elements.
Datta wants a stopwatch at the box office counter, to time each booking. Start starts an interval, Stop must clear it. But Start and Stop are clicked in different renders, and every local variable is made again in each render, so the interval's id that Start got is gone by the time Stop runs. State would keep it, but then every Start would cause a render for a number nobody sees.
And on the test site, the waitlist button for a sold-out show does something odd: the console says the count went up, and the page does not. That one is a value kept in the wrong place.
What these need is a place React keeps between renders, like state, that does not cause a render when it changes. That is a ref.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
SearchBox.jsx: press / to search
SearchBox shows Ira's search input, the SearchField component from SearchField.jsx, with id show-search, label Search shows and placeholder Film, play or artist. SearchField takes a ref prop and gives it to its input.
Put the focus in that search box when SearchBox first appears, and again whenever someone presses the / key anywhere on the page while the focus is not in the box; then the / must not be typed into the box (stop the key's default action). When the focus is already in the box, / is typed as usual: typing a/b in the box gives a/b. Listen for keydown on window or document, and remove the listener when SearchBox goes away. The Check renders SearchBox in StrictMode.
- Task 2
Stopwatch.jsx: Datta's counter stopwatch
Make Stopwatch show, in this order: a heading h2 that says Time at the counter; a paragraph with the time as minutes:seconds, 0:00 at first, then 0:01, 0:02 and so on (65 seconds is 1:05); and three buttons, Start, Stop and Reset. Start starts counting, one second at a time, and is disabled while the stopwatch runs. Stop stops counting and keeps the time, and is disabled while the stopwatch is stopped. Reset stops counting and sets the time back to 0:00. Start after Stop goes on from the time shown. Give the time paragraph className="clock" for app.css.
Stop must clear the interval that Start made, and when Stopwatch goes away while it runs, its interval must be cleared too. The Check uses fake timers in StrictMode: it clicks Start and waits 3 seconds (0:03), clicks Stop and waits 4 seconds (still 0:03, and no interval left running), clicks Start and waits 62 seconds (1:05), clicks Reset (0:00, and nothing running), then clicks Start and removes Stopwatch.
- Task 3
SeatRow.jsx: the picked seat scrolls into view
SeatRow gets four props: seats, the row's seat ids in order (F1 to F24); taken, the ids already booked (F4, F5, F14, F18, F19 and F24); picked, the picked seat's id or null; and onPick, a function to call with a seat's id. Show one button for each seat, in order, that says its id. A taken seat's button is disabled. The picked seat's button has aria-pressed true, and every other button aria-pressed false. Clicking a free seat calls onPick once, with its id. Give the buttons' parent className="seats" for app.css.
Whenever picked changes, and when SeatRow first shows with a picked seat, call scrollIntoView on the picked seat's own button, with exactly the option { block: 'nearest', inline: 'center' }. The Check replaces scrollIntoView to watch which button it is called on: it changes picked to F20, then to F2, and also shows SeatRow with picked F22 from the start.
- Task 4 · interview question
Interview: WaitlistButton.jsx, the count that never moves
WaitlistButton gets the number of people already waiting in its waiting prop, and shows a paragraph 37 people are waiting (the number from waiting) and a button Join the waitlist. After one click, the paragraph must say 38 people are waiting (one more), and the button must say You are on the waitlist and be disabled.
The code keeps the count and whether you joined in refs. Click it in the Preview: the console says joined, count is 38, but the page still says 37 and the button has not changed. Fix it so the page changes after the click. The Check renders WaitlistButton with waiting 37 and with waiting 5 (then 6 people are waiting after the click).
Rukhsana will ask: when do you keep a value in a ref, and when in state?
What you will learn
- useRef: a box, { current: ... }, that React keeps between renders; changing it does not render again.
- Refs to elements: the ref attribute, filled in with the real element after React puts it on the page.
- focus() and scrollIntoView(): what you do with an element, from an effect or a handler.
- ref is a prop in React 19: your own component takes it and passes it on.
- Ids in a ref: an interval's or a timeout's id, so another click can clear it.
- Never during render: read and write refs in handlers and effects only.
- Ref or state: shown on the page, state; kept out of sight, a ref.
More in Effects and data
Build "Refs" yourself
Parda sells tickets for Pune's own cinemas and stages, and its booking page broke on opening night. You rebuild it in React. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of React are free. This one comes with the all labs plan.