react shows error when submit button click

Rahul :

I am new to React. I don't understand why it showing error. I need to repeat the array object but when It reaches the last array it does not restart.https://codesandbox.io/s/generate-quote-xpu1q https://i.stack.imgur.com/W7Tqq.png

Thanveer Shah :

It is because you are not checking if the array has reached the last item in the button click, So if you don't check it in the button click then it will increment the index again before it even gets to check , So change your code to this:

const qutoesBtn = () => {
    if (index === data.length - 1) {
      setIndex(0);
    } else {
      setIndex(index + 1);
    }
  };

Here -1 refers to the last item in the array

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=410659&siteId=1