Close the earlier lessons and build this from the brief. This time the API is
fixed: App.jsx already uses Accordion the way the brief describes, and
useControllableState is included. Write Accordion.jsx so it works.
The brief
<Accordion>holds any number of<AccordionItem value title>. Several items can be open at once.- Uncontrolled:
defaultOpen={["reducers"]}sets which items start open. - Controlled:
open={[...]}andonOpenChange={setOpen}let the parent own the open items, and change them, as Close all from the parent does. - Each item's title is a
<button>inside an<h3>, witharia-expanded, andaria-controlspointing at its panel, usinguseId. Its content is in the panel, shown only when open. - An
AccordionItemoutside anAccordionthrows <AccordionItem> must be used inside <Accordion>.
Acceptance checks:
- In the controlled accordion, opening Suspense shows Open: context, suspense. Close all from the parent closes both and shows Open: none.
- The uncontrolled accordion starts with Reducers open, and doesn't affect the Open: line.
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Hint 1: the parts
Accordion holds the open values with useControllableState, and shares them
and a toggle(value) function through a context. AccordionItem reads the
context to know whether it's open.
Hint 2: toggling a value in an array
Remove it if it's there, add it if it isn't:
openValues.includes(value) ? openValues.filter((v) => v !== value) : [...openValues, value].
Show one solution
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Check yourself
- When is a compound component better than a single component with many props?
- How does a component tell whether it's controlled or uncontrolled?
- When is an imperative handle the right choice, and when is a prop better?
Completion is saved in this browser only. It does not measure mastery.