Skip to content

Commit 780259a

Browse files
committed
chore: Refactor Accordion component to use state hook for panel activation
1 parent 01edd5c commit 780259a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/content/learn/sharing-state-between-components.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,29 @@ In this example, it's the `Accordion` component. Since it's above both panels an
133133
import { useState } from 'react';
134134
135135
export default function Accordion() {
136+
const [isActive, setIsActive] = useState(false);
137+
136138
return (
137139
<>
138140
<h2>Almaty, Kazakhstan</h2>
139-
<Panel title="About" isActive={true}>
141+
<Panel title="About" isActive={isActive} onShow={() => setIsActive(true)}>
140142
With a population of about 2 million, Almaty is Kazakhstan's largest city. From 1929 to 1997, it was its capital city.
141143
</Panel>
142-
<Panel title="Etymology" isActive={true}>
144+
<Panel title="Etymology" isActive={isActive} onShow={() => setIsActive(true)}>
143145
The name comes from <span lang="kk-KZ">алма</span>, the Kazakh word for "apple" and is often translated as "full of apples". In fact, the region surrounding Almaty is thought to be the ancestral home of the apple, and the wild <i lang="la">Malus sieversii</i> is considered a likely candidate for the ancestor of the modern domestic apple.
144146
</Panel>
145147
</>
146148
);
147149
}
148150
149-
function Panel({ title, children, isActive }) {
151+
function Panel({ title, children, isActive, onShow }) {
150152
return (
151153
<section className="panel">
152154
<h3>{title}</h3>
153155
{isActive ? (
154156
<p>{children}</p>
155157
) : (
156-
<button onClick={() => setIsActive(true)}>
158+
<button onClick={onShow}>
157159
Show
158160
</button>
159161
)}

0 commit comments

Comments
 (0)