UNPKG

rooks

Version:

Essential React custom hooks ⚓ to super charge your components!

25 lines (24 loc) 776 B
import { useCallback, useState } from "react"; /** * useSelect hook * Helps easily select a value from a list of values * * @param list List of values to select a value from * @param {number} initialIndex Initial index which is selected * @returns handler * @see https://react-hooks.org/docs/useSelect */ function useSelect(list, initialIndex) { if (initialIndex === void 0) { initialIndex = 0; } var _a = useState(initialIndex), selectedIndex = _a[0], setSelectedIndex = _a[1]; var setItem = useCallback(function (item) { setSelectedIndex(list.indexOf(item)); }, [list]); return { index: selectedIndex, item: list[selectedIndex], setIndex: setSelectedIndex, setItem: setItem, }; } export { useSelect };