real trace · react
04. React internals / Lesson 3 of 6 · intermediate · 8 min · 10 steps

Why it re-rendered

Why one key press renders a whole inbox.

support · inboxCOMPONENT TREE1 renderedInbox×1first render

My inbox feels slow when I type a reply. One key should only touch the reply box. Let me count what renders.

IN THIS LESSONStep 1 of 10 · Mei's slow inbox0:00 / 2:00

Transcript

follows the lesson · 0 of 10 steps

Why it re-rendered: the whole lesson in words

Typing a reply in a support inbox re-renders every ticket row.

The one-page cheatsheet for this lesson →

Sources

Inbox.jsx · agent-writtenjsx
import { useState } from 'react';
const tickets = [
{ id: 't1', subject: 'Refund not received' },
{ id: 't2', subject: 'Order shows twice' },
{ id: 't3', subject: 'Cannot reset password' },
{ id: 't4', subject: 'Wrong size delivered' },
{ id: 't5', subject: 'Invoice address' },
];
function TicketRow({ ticket, open, onOpen }) {
return <li><button aria-pressed={open} onClick={onOpen}>{ticket.subject}</button></li>;
}
function TicketList({ tickets, openId, onOpen }) {
return (
<ul>
{tickets.map((t) => (
<TicketRow key={t.id} ticket={t} open={t.id === openId} onOpen={() => onOpen(t.id)} />
))}
</ul>
);
}
function ReplyBox({ draft, onDraft }) {
return <textarea aria-label="Reply" value={draft} onChange={(e) => onDraft(e.target.value)} />;
}
export default function Inbox() {
const [openId, setOpenId] = useState('t1');
const [draft, setDraft] = useState('');
return (
<main>
<TicketList tickets={tickets} openId={openId} onOpen={setOpenId} />
<ReplyBox draft={draft} onDraft={setDraft} />
</main>
);
}
CHEATSHEET

Opens after you lock in your predictions. It states the answers.