Skip to content

useCounter โ€‹

A React hook for managing numeric state with increment, decrement, reset, and custom set capabilities.


๐Ÿงฉ Overview โ€‹

useCounter simplifies handling numeric state, especially for counters, pagination, score tracking, or quantity adjustments.
It provides helpful methods for incrementing, decrementing, resetting, and directly setting values โ€” with optional min, max, and step limits.


๐Ÿš€ Usage โ€‹

tsx
import { useCounter } from "react-hookstack";

function CounterExample() {
  const { count, increment, decrement, reset, set } = useCounter(0, {
    min: 0,
    max: 10,
    step: 2,
  });

  return (
    <div>
      <button onClick={decrement}>-</button>
      <span>{count}</span>
      <button onClick={increment}>+</button>
      <button onClick={reset}>Reset</button>
      <button onClick={() => set(5)}>Set to 5</button>
    </div>
  );
}

โš™๏ธ API Reference โ€‹

useCounter(initialValue?: number, options?: UseCounterOptions): UseCounterReturn

Parameters โ€‹

NameTypeDefaultDescription
initialValuenumber0The initial counter value.
optionsUseCounterOptions{}Optional settings for min, max, and step.

UseCounterOptions โ€‹

PropertyTypeDefaultDescription
minnumberundefinedMinimum allowed value.
maxnumberundefinedMaximum allowed value.
stepnumber1The step size for increment/decrement.

Returns โ€‹

PropertyTypeDescription
countnumberThe current counter value.
increment() => voidIncreases the counter by the defined step (up to max if provided).
decrement() => voidDecreases the counter by the defined step (down to min if provided).
reset() => voidResets the counter to its initial value.
set(value: number) => voidManually sets the counter to a specific value.

๐Ÿง  Example Use Cases โ€‹

  • Managing quantity selectors in shopping carts
  • Tracking pagination or current step in a flow
  • Implementing score counters in games or quizzes
  • Building numeric input controls with step adjustments

๐Ÿ’ก Tips โ€‹

  • Use min and max to keep your counter within safe bounds.
  • Adjust step for smoother or larger increments.
  • Combine with useBoolean to toggle enabled/disabled state for controls.

๐Ÿงฑ Implementation โ€‹

tsx
import { useCallback, useState } from "react";

export interface UseCounterOptions {
  min?: number;
  max?: number;
  step?: number;
}

export interface UseCounterReturn {
  count: number;
  increment: () => void;
  decrement: () => void;
  reset: () => void;
  set: (value: number) => void;
}

export function useCounter(
  initialValue: number = 0,
  options: UseCounterOptions = {}
): UseCounterReturn {
  const { min, max, step = 1 } = options;
  const [count, setCount] = useState<number>(initialValue);

  const increment = useCallback(() => {
    setCount((prev) => {
      const next = prev + step;
      if (max !== undefined && next > max) return max;
      return next;
    });
  }, [max, step]);

  const decrement = useCallback(() => {
    setCount((prev) => {
      const next = prev - step;
      if (min !== undefined && next < min) return min;
      return next;
    });
  }, [min, step]);

  const reset = useCallback(() => setCount(initialValue), [initialValue]);
  const set = useCallback((value: number) => setCount(value), []);

  return { count, increment, decrement, reset, set };
}

export default useCounter;

๐Ÿงพ Type Definition โ€‹

tsx
type UseCounterReturn = {
  count: number;
  increment: () => void;
  decrement: () => void;
  reset: () => void;
  set: (value: number) => void;
}