'sliders: increase state in setInterval with a counter between 1 and x

I want to make sliding between 3 images in setInterval, I can increase the count of image but when the count equals 4, the code doesn't set the count to 1... it still increase more than 3..

==========================================================================

const [idSliderActive, setIdSliderActive] = useState(1)

useEffect(() => { const timer = setInterval(() => {

  setIdSliderActive((idSliderActive === 4) ? 1 : idSliderActive => idSliderActive + 1)
  console.log(idSliderActive)
  
}, 1000)

return () => clearInterval(timer)

}, [])



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source