# `useStateList` Provides handles for circular iteration over states list. Supports forward and backward iterations and arbitrary position set. ## Usage ```jsx import { useStateList } from 'react-use'; import { useRef } from 'react'; const stateSet = ['first', 'second', 'third', 'fourth', 'fifth']; const Demo = () => { const { state, prev, next, setStateAt, setState, currentIndex, isFirst, isLast } = useStateList(stateSet); const indexInput = useRef(null); const stateInput = useRef(null); return (
        {state} [index: {currentIndex}], [isFirst: {isFirst}], [isLast: {isLast}]
      



); }; ``` ## Reference ```ts const { state, currentIndex, prev, next, setStateAt, setState, isFirst, isLast } = useStateList(stateSet: T[] = []); ``` If `stateSet` changed, became shorter than before and `currentIndex` left in shrunk gap - the last element of list will be taken as current. - **`state`**_`: T`_ — current state value; - **`currentIndex`**_`: number`_ — current state index; - **`prev()`**_`: void`_ — switches state to the previous one. If first element selected it will switch to the last one; - **`next()`**_`: void`_ — switches state to the next one. If last element selected it will switch to the first one; - **`setStateAt(newIndex: number)`**_`: void`_ — set the arbitrary state by index. Indexes are looped, and can be negative. _4ex:_ if list contains 5 elements, attempt to set index 9 will bring use to the 5th element, in case of negative index it will start counting from the right, so -17 will bring us to the 4th element. - **`setState(state: T)`**_`: void`_ — set the arbitrary state value that exists in `stateSet`. _In case new state does not exists in `stateSet` an Error will be thrown._ - **`isFirst`**_`: boolean`_ — `true` if current state is the first one. - **`isLast`**_`: boolean`_ — `true` if current state is the last one.