Hex Core JS
    Preparing search index...

    Function useCount

    • Provides a numeric state variable with increment and decrement utility functions.

      Parameters

      • initialValue: number | (() => number) = 0

        The initial value of the count variable.

      • options: UseCountOptions = {}

        Options for handling of the counter. Can be either min/max bounds or a mod bound.

      Returns {
          count: number;
          decrement: (
              amount?: number | Event | SyntheticEvent<Element, Event>,
          ) => void;
          increment: (
              amount?: number | Event | SyntheticEvent<Element, Event>,
          ) => void;
          setCount: (action: SetStateAction<number>) => void;
      }

      The given initial value will be bounded according to the bounding options if given.

      const { count, increment, decrement, setCount } = useCount();

      increment(); // count goes up by 1
      increment(10); // count goes up by 10

      decrement(); // count goes down by 1
      decrement(10); // count goes down by 10

      setCount(100); // count set to 100
      const { count, increment, decrement, setCount } = useCount(0, { min: 0, max: 10 });

      decrement(); // count stays at 0

      increment(100); // count goes to 10

      setCount(-10); // count goes to 0
      const { count, increment, decrement, setCount } = useCount(0, { mod: 3 });

      decrement(); // count goes to 2

      increment(); // count goes to 0

      setCount(100); // count goes to 1