@supunlakmal/hooks
Version:
A collection of reusable React hooks
21 lines (20 loc) • 927 B
TypeScript
/**
* @name useCycle
* @description - Hook that cycles through a predefined list of values.
*
* @template T The type of the values in the list.
* @param {T[]} items An array of items to cycle through.
* @param {number} [initialIndex=0] The index of the item to start with.
* @returns {[T, { next: () => void; prev: () => void; goTo: (index: number) => void; }]} A tuple containing the current item and an object with functions to cycle (`next`, `prev`) or jump (`goTo`) to a specific item.
* @throws If the items array is empty.
* @throws If the initialIndex is out of bounds.
*
* @example
* const [currentTheme, { next: cycleTheme, prev: previousTheme }] = useCycle(['light', 'dark']);
* const [currentValue, actions] = useCycle([10, 20, 30], 1);
*/
export declare const useCycle: <T>(items: T[], initialIndex?: number) => [T, {
next: () => void;
prev: () => void;
goTo: (index: number) => void;
}];