samedi 14 mars 2020

What is the proper way to use If conditional?

I am trying to set auto index in React like below: You can think, now the total index of my data 5. It can be changed at any time.

const [index, setIndex] = useState(0);
    const indexSetter = ()=>{
       if(index===data.length-1){
            setIndex(0)
        }else{
            setIndex(index+1)}
    }
    useEffect(()=>{
        setInterval(indexSetter, 5000);
    })

But after rounding total length, it crashes. It doesn't set index again from 0; I searched on google. I don't understand what mistake I have done. Since I am new in coding, Don't be bothered by my boring question.

Aucun commentaire:

Enregistrer un commentaire