Course contentsShow
← React
Lesson 2318 of 2,39650. Performance: Optimization TechniquesFree lesson

Local State for UI-Only Concerns

Keeping accordion open states, modal visibility, and other UI state as local as possible.

Local State for UI-Only Concerns

You'll learn why keeping transient UI state (like accordion toggles or modal visibility) as close to the component using it prevents unnecessary re-renders across your app.

The Core Idea

Not all state belongs in a shared store or high up in your component tree. UI-only concerns—whether an accordion is open, a dropdown is expanded, or a tooltip is visible—should typically live in local component state using useState.

When you hoist this kind of state too high, every state change forces parent components and their entire subtrees to re-render, even if 99% of your app doesn't care whether AccordionItem #3 is open or closed.

Why Local State Wins

Think of UI state like a light switch in a room. You don't need to alert the entire building's electrical system every time someone flips it. Similarly, an accordion's isOpen state only matters to that accordion component—not to sibling components, not to the page container, and definitely not to a global state manager.

// ✅ Good: Local state
function AccordionItem({ title, content }) {
  const [isOpen, setIsOpen] = useState(false);
  
  return (
    <div>
      <button onClick={() => setIsOpen(!isOpen)}>
        {title}
      </button>
      {isOpen && <div>{content}</div>}
    </div>
  );
}

Compare this to lifting isOpen to a parent managing 20 accordion items—every toggle would re-render all 20, even though only one changed.

When to Keep State Local

  • Modal/dialog visibility
  • Form field focus states
  • Hover effects and tooltips
  • Dropdown open/closed state
  • Animation or transition flags

Key Takeaway: Keep UI-only state in the component that renders it—you'll minimize re-render scope and keep your performance profile clean without needing memo or other optimizations.