Skip to content

Commit 8ee73c6

Browse files
committed
Fix out-of-bounds error on the page /learn/state-a-components-memory
1 parent f3d9794 commit 8ee73c6

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

src/content/learn/state-a-components-memory.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ const [index, setIndex] = useState(0);
190190

191191
> The `[` and `]` syntax here is called [array destructuring](https://javascript.info/destructuring-assignment) and it lets you read values from an array. The array returned by `useState` always has exactly two items.
192192
193-
This is how they work together in `handleClick`:
193+
This is how they work together in `handleClick` (`% sculptureList.length` is used to return to the first item when the index exceeds the list index):
194194

195195
```js
196196
function handleClick() {
197-
setIndex(index + 1);
197+
setIndex((index + 1) % sculptureList.length);
198198
}
199199
```
200200

@@ -210,7 +210,7 @@ export default function Gallery() {
210210
const [index, setIndex] = useState(0);
211211

212212
function handleClick() {
213-
setIndex(index + 1);
213+
setIndex((index + 1) % sculptureList.length);
214214
}
215215

216216
let sculpture = sculptureList[index];
@@ -394,7 +394,7 @@ export default function Gallery() {
394394
const [showMore, setShowMore] = useState(false);
395395

396396
function handleNextClick() {
397-
setIndex(index + 1);
397+
setIndex((index + 1) % sculptureList.length);
398398
}
399399

400400
function handleMoreClick() {
@@ -759,7 +759,7 @@ export default function Gallery() {
759759
const [showMore, setShowMore] = useState(false);
760760

761761
function handleNextClick() {
762-
setIndex(index + 1);
762+
setIndex((index + 1) % sculptureList.length);
763763
}
764764

765765
function handleMoreClick() {
@@ -930,7 +930,7 @@ export default function Gallery() {
930930
const [showMore, setShowMore] = useState(false);
931931

932932
function handleNextClick() {
933-
setIndex(index + 1);
933+
setIndex((index + 1) % sculptureList.length);
934934
}
935935

936936
function handleMoreClick() {
@@ -1082,7 +1082,7 @@ export default function Gallery() {
10821082

10831083
function handleNextClick() {
10841084
if (hasNext) {
1085-
setIndex(index + 1);
1085+
setIndex((index + 1) % sculptureList.length);
10861086
}
10871087
}
10881088

0 commit comments

Comments
 (0)