Skip to content

Commit 2d4882e

Browse files
committed
cycle through array with mod
1 parent aef0348 commit 2d4882e

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/components/Practice.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ export class Practice extends React.Component {
1010
this.setState(state => ({ isFront: !state.isFront }))
1111
}
1212

13+
handleNextCard = () => {
14+
const { cards } = this.props
15+
this.setState(state => ({
16+
currentIndex: (state.currentIndex + 1) % cards.length
17+
}))
18+
}
19+
20+
handlePrevCard = () => {
21+
const { cards } = this.props
22+
this.setState(state => ({
23+
currentIndex: (state.currentIndex - 1 + cards.length) % cards.length
24+
}))
25+
}
26+
1327
render() {
1428
const { cards } = this.props
1529
const { currentIndex, isFront } = this.state
@@ -32,10 +46,18 @@ export class Practice extends React.Component {
3246
{isFront ? 'show back' : 'show front'}
3347
</button>
3448
<div>
35-
<button type="button" className="secondary">
49+
<button
50+
type="button"
51+
className="secondary"
52+
onClick={this.handlePrevCard}
53+
>
3654
previous
3755
</button>
38-
<button type="button" className="primary">
56+
<button
57+
type="button"
58+
className="primary"
59+
onClick={this.handleNextCard}
60+
>
3961
next
4062
</button>
4163
</div>

0 commit comments

Comments
 (0)