Lifting State Up

Two panels that must change together share state kept in their parent. Opening one closes the other.

React · beginner · 7 min · 18 steps · free

What you'll learn

LiftingState.jsx

import { useState } from 'react';

function Panel({ title, isActive, onShow, children }) {
  return (
    <section>
      <h3>{title}</h3>
      {isActive ? <p>{children}</p> : <button onClick={onShow}>Show</button>}
    </section>
  );
}

export default function Accordion() {
  const [activeIndex, setActiveIndex] = useState(0);

  return (
    <div>
      <Panel title="About" isActive={activeIndex === 0} onShow={() => setActiveIndex(0)}>
        We teach code with pictures.
      </Panel>
      <Panel title="Contact" isActive={activeIndex === 1} onShow={() => setActiveIndex(1)}>
        Write to us any time.
      </Panel>
    </div>
  );
}

Step by step

  1. React renders <Accordion> for the first time: it calls the Accordion function.
  2. useState runs for the first time, so React stores the starting value for activeIndex and returns it: 0.
  3. The state lives in Accordion, the parent. Each Panel gets isActive and onShow as props.
  4. The first Panel gets isActive = true, so it will show its text.
  5. Panel returns JSX: a description of what the screen should show, <section>…</section>. React has not changed the page yet.
  6. The second gets isActive = false, so it will show a Show button.
  7. Panel returns JSX: a description of what the screen should show, <section>…</section>. React has not changed the page yet.
  8. React turns the JSX into real page elements and shows them: look at the Screen panel.
  9. You click Show in the Contact panel. Panel has no state of its own: its button calls the onShow prop, the arrow from Accordion.
  10. setActiveIndex(1) does not change activeIndex right away: React queues the update and will render again. In this call activeIndex is still 0.
  11. React renders <Accordion> again (render 2): it calls Accordion once more, from the top.
  12. useState returns the value React has kept for activeIndex: 1. The starting value is only used on the first render.
  13. Accordion returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  14. Accordion rendered with activeIndex = 1, so its Panels render again with new props.
  15. Panel returns JSX: a description of what the screen should show, <section>…</section>. React has not changed the page yet.
  16. React renders <Panel> again (render 2): it calls Panel once more, from the top. Its props: title = "Contact", isActive = true, onShow = ƒ () => setActiveIndex(1).
  17. Panel returns JSX: a description of what the screen should show, <section>…</section>. React has not changed the page yet.
  18. About collapses and Contact opens, because both read the same state.

Common mistakes

Best practices

Key takeaways

Check yourself

Panel calls onShow. Whose state changes?

Answer: Accordion's. onShow was passed down from Accordion.

Why not give each Panel its own state?

Answer: They could not close each other

·

More in State and Events