UNPKG

@mantine/hooks

Version:

A collection of 50+ hooks for state and UI management

50 lines (49 loc) 1.31 kB
"use client"; let react = require("react"); //#region packages/@mantine/hooks/src/use-state-history/use-state-history.ts function useStateHistory(initialValue) { const [state, setState] = (0, react.useState)({ history: [initialValue], current: 0 }); const set = (0, react.useCallback)((val) => setState((currentState) => { const nextState = [...currentState.history.slice(0, currentState.current + 1), val]; return { history: nextState, current: nextState.length - 1 }; }), []); const back = (0, react.useCallback)((steps = 1) => setState((currentState) => ({ history: currentState.history, current: Math.max(0, currentState.current - steps) })), []); const forward = (0, react.useCallback)((steps = 1) => setState((currentState) => ({ history: currentState.history, current: Math.min(currentState.history.length - 1, currentState.current + steps) })), []); const reset = (0, react.useCallback)(() => { setState({ history: [initialValue], current: 0 }); }, [initialValue]); const handlers = (0, react.useMemo)(() => ({ back, forward, reset, set }), [ back, forward, reset, set ]); return [ state.history[state.current], handlers, state ]; } //#endregion exports.useStateHistory = useStateHistory; //# sourceMappingURL=use-state-history.cjs.map