use-iterator
iterates over a collection
Install
yarn add @gdo-bzh/use-iterator react @gdo-bzh/use-range-iterator
# or
npm i @gdo-bzh/use-iterator react @gdo-bzh/use-range-iterator -S
Usage
import React from 'react'
const Example = () => {
const { item, next, previous, hasNext, hasPrevious } = useRangeIterator({ items:[{...}, {...}] })
}
Types
export type State<T> = {
/**
* start value
*/
items: T[]
/**
* the number of values to navigate at a time
*/
step: number
/**
* true will make navigation cycling
*/
loop: boolean
}
export type UseIterator = {
<T>(initialState: Partial<Omit<State<T>, 'value' | 'items'>> & Pick<State<T>, 'items'>): {
loop: State<T>['loop']
step: State<T>['step']
item: T
hasNext: boolean
/**
* returns true if the iteration has more Element
*/
hasPrevious: boolean
/**
* iterates to the next element
*/
next: () => void
/**
* iterates to the previous element
*/
previous: () => void
}
}
License
MIT © gdo-bzh