I want to make a repeated array like this
1->2->3->1->2->3.....
const [current,setCurrent] = useState(0);
const [direction,setDirection] = useState()
const prevCurrent = usePrevious(current)
const maxData = 3;
function handler(val){
if(val==='plus'){
current !== maxData ? setCurrent(prev=>prev+1):setCurrent(0)
}else{
current !== 0 ? setCurrent(prev=>prev-1):setCurrent(maxData)
}
}
useEffect(()=>{
if(current>prev){
setDirection('next')
} else{
setDirection('prev')
}
},[current])
return <div>
<button onClick={()=>handler('plus')}>+</button>
<button onClick={()=>handler('min')}>-</button>
<span>{direction}</span>
</div>
In this case, I always want to repeat my current, example if the new current is 2 and prev current is 1 then the direction will be next, but when the current is the last step and the current will set to 0, but the direction will be set to 'prev' because current < prev, but I want my direction still set to next.
Is there some algorithm to fix this one?