React Dojo

Search

Search concepts, exercises and quizzes

practicebasic

Accordion

An accordion component where each item can be expanded or collapsed independently. The state is a Set of open IDs.


Objectives

  1. 1.Declare openItems state as an empty new Set()
  2. 2.In toggleItem: if id is in the Set, remove it; otherwise add it — always using a functional update with a copy
  3. 3.Pass openItems.has(item.id) as the isOpen prop to each AccordionItem
  4. 4.The icon rotates 180° when isOpen is true
  5. 5.The content only renders when isOpen is true
your code
Hint

Use a Set to store open IDs. To toggle: if set.has(id) delete it, otherwise add it. Always copy the Set before mutating.

Related concepts

Was this helpful?
Sign in to give feedback