Keys and Component Identity

Type a message to Ada, then switch to Linus. A key makes React start a fresh Chat so the draft is not sent to the wrong person.

React · intermediate · 8 min · 22 steps · free

What you'll learn

KeysIdentity.jsx

import { useState } from 'react';

function Chat({ contact }) {
  const [draft, setDraft] = useState('');

  return (
    <div>
      <p>To: {contact}</p>
      <input value={draft} onChange={(e) => setDraft(e.target.value)} placeholder="Message" />
    </div>
  );
}

export default function Messenger() {
  const [to, setTo] = useState('Ada');

  return (
    <div>
      <button onClick={() => setTo(to === 'Ada' ? 'Linus' : 'Ada')}>Switch contact</button>
      <Chat key={to} contact={to} />
    </div>
  );
}

Step by step

  1. React renders <Messenger> for the first time: it calls the Messenger function.
  2. useState runs for the first time, so React stores the starting value for to and returns it: "Ada".
  3. Messenger returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  4. React renders <Chat> for the first time: it calls the Chat function. Its props: contact = "Ada".
  5. useState runs for the first time, so React stores the starting value for draft and returns it: "".
  6. Chat returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  7. React turns the JSX into real page elements and shows them: look at the Screen panel.
  8. You type “Hi Ada” into the input. React calls its onChange handler.
  9. setDraft(e.target.value) does not change draft right away: React queues the update and will render again. In this call draft is still "".
  10. Chat re-renders with the draft "Hi Ada" in its state.
  11. useState returns the value React has kept for draft: "Hi Ada". The starting value is only used on the first render.
  12. Chat returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  13. React compares the new JSX with the last one and updates only what changed on the page.
  14. You switch to Linus.
  15. setTo(to === 'Ada' ? 'Linus' : 'Ada') does not change to right away: React queues the update and will render again. In this call to is still "Ada".
  16. React renders <Messenger> again (render 2): it calls Messenger once more, from the top.
  17. useState returns the value React has kept for to: "Linus". The starting value is only used on the first render.
  18. Messenger returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  19. The key changed from "Ada" to "Linus", so React treats this as a different Chat: it removes the old one and creates a new one with fresh state.
  20. draft starts again at "".
  21. Chat returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  22. The draft is empty for Linus. The old Chat, key "Ada", is gone.

Common mistakes

Best practices

Key takeaways

Check yourself

<Chat contact={to} /> with a new contact: draft?

Answer: Kept. Same place, same type.

How do you reset a component's state on purpose?

Answer: Change its key

·

More in How Rendering Works