React · Chapter 15 of 26
You might not need an effect
By the end of this chapter you can tell when code belongs in an effect and when it does not; replace an effect that works out a value with a calculation during render; move code that answers a user's action into the event handler; reset a component with a key instead of an effect; and measure what an unneeded effect costs: an extra render, and a page that shows the wrong value for a moment. 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
Chapter 14 gave you effects, and Rukhsana's review of the week's code found them everywhere. The list of shows filled itself in with an effect, so it flashed Showing 0 of 12 before every list. A receipt sent the booking from an effect, so in development every click on Pay sent two bookings, and Datta's test bookings came in pairs. The member editor reset its form with an effect, so for one render it showed one member's typed email under another member's name. And the seat row had three effects in a chain, so the first seat you picked made the page render three times, one after the other.
None of that code talks to anything outside React. It only moves values from one piece of state to another, or reacts to a click late. Rukhsana's rule for the team: an effect is for keeping something outside React in step. Everything else has a better place, and this chapter is about finding it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
FilteredShows.jsx: the list without an effect
FilteredShows({ shows }) keeps the shown list in state and fills it in with an effect, so its first render shows Showing 0 of 12 and an empty list, and every click on a kind takes two renders. Rewrite it with no effect and no state for the list.
It still shows five buttons, All, Films, Plays, Comedy and Music, where the chosen one has aria-pressed true (All at the start); a paragraph Showing 4 of 12, the number of shows of the chosen kind and the number of all the shows; and a list with one item per show of that kind, its title, in the order of shows. Films are the shows whose kind is film, Plays play, Comedy comedy and Music music.
The Check gives FilteredShows its own 8 shows and counts renders with React's Profiler: the first render must already show the whole list, and a click on a kind must take one render.
- Task 2
Checkout.jsx: one click, one booking
Checkout({ seats, price, onBook }) shows a heading, h2, Your seats; a paragraph with the seats joined by a comma and a space (E7, E8); and a button Pay ₹360, the number of seats times price. After Pay, instead of the paragraph and the button, it shows a paragraph Booked: E7, E8. ₹360 paid.
Now Receipt sends the booking with onBook from an effect when it appears, so under StrictMode one click sends two bookings. Fix it: Checkout calls onBook({ seats, total }) exactly once per click on Pay, with total the seats times price, and never before the click. Receipt only shows what was booked. The Check renders Checkout under StrictMode with three seats at ₹240 (Pay ₹720) and counts the calls.
- Task 3
MemberEditor.jsx: the form that resets itself
MemberEditor({ members }) shows a heading, h2, Members; one button per member, with the name, where the chosen one has aria-pressed true (the first member at the start); and ContactForm for the chosen member: a box labelled Email for and the member's name (Email for Aditya Bapat), which starts with the member's email, and, while the box differs from that email, a paragraph Changed, not saved yet.
ContactForm resets its draft with an effect when the member changes, so for one render the new member's box holds the old member's draft, and every switch takes two renders. Fix it with no useEffect left in MemberEditor.jsx: switching to another member shows that member's email at once, in one render, and switching back shows the first member's email again, not what was typed. The Check gives its own two members and counts renders with React's Profiler.
- Task 4 · interview question
Interview: PickSeats.jsx, three effects, one needed
PickSeats({ seats, price }) lets you pick seats in a row. Its three effects work out the total, then whether you may pay, then put the total in document.title, so the first pick takes three renders. Keep only the effect that is needed, so that every pick takes one render.
It shows one button per seat in seats (E7, E8 ...), where a picked seat has aria-pressed true and a click picks or unpicks it; a paragraph, No seats picked, or the number picked and that times price, as 2 seats: ₹360 (1 seat: ₹180 for one); and a button Pay ₹360, disabled with no seats picked or with more than 4. document.title is ₹360 · Parda while seats are picked, and Parda with none, set after React has updated the page, never while rendering. The Check counts renders with React's Profiler, gives its own seats at ₹250, and reads document.title after each click.
Rukhsana will ask: when do you need an effect?
What you will learn
- What an unneeded effect costs: an extra render, and a page that is wrong for a moment (measured).
- Work it out during render: values from props and state need no state and no effect.
- Do it in the event handler: what happens because the user did something belongs where you know what they did.
- Tell the parent in the handler: not from an effect that watches your own state.
- Reset with a key: a component that must start again for new props gets a new key.
- Chains of effects: each link costs one more render.
- When you do need an effect: the questions to ask.
More in Effects and data
Build "You might not need an effect" 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.