import { Dispatch , SetStateAction , useState } from 'react' ; export interface UseCycleOutput { currentItem : any ; getNextIndex : ( ) => void ; getPrevIndex : ( ) => void ; hasEnded : boolean ; currentIndex : number ; isLastItem : boolean ; setCurrentIndex : Dispatch < SetStateAction < number > > ; } export default function useCycle ( items ) { const [ currentIndex , setCurrentIndex ] = useState ( 0 ) ; return { setCurrentIndex , currentItem : items [ currentIndex ] , getNextIndex : ( ) => { setCurrentIndex ( currentIndex + 1 ) ; } , getPrevIndex : ( ) => { setCurrentIndex ( currentIndex - 1 ) ; } , hasEnded : currentIndex === items . length , currentIndex , isLastItem : currentIndex === items . length - 1 } ; }