useCounter
Manages a numeric counter state with optional min/max clamping and helper methods.
Loading preview...
The useCounter hook manages a numeric state with optional min and max boundaries and exposes memoized helpers (inc, dec, set, and reset) to update the value.
Installation
Usage
Examples
Bounded Quantity Picker
Pass min and max options to clamp the counter so it never exceeds the allowed range.
Custom Step Sizes and Direct Assignment
Pass a custom delta to inc or dec, or call set with a number or updater function.
API Reference
UseCounterOptions
Optional boundary constraints for the counter.
Property
Specification
min?number
Minimum allowed counter value
Full Signature
max?number
Maximum allowed counter value
Full Signature
UseCounterReturn
Counter value and helper methods returned by the hook.
Property
Specification
countnumber
Current counter value
incfunctiondefault:1
Increments the counter by
deltaFull Signature
Parameters
delta?numberdecfunctiondefault:1
Decrements the counter by
deltaFull Signature
Parameters
delta?numbersetfunction
Sets the counter to a specific value or updater function (clamped to
min/max)Full Signature
Parameters
valueSetStateAction<number>resetfunction
Resets the counter back to
initialValue (clamped to min/max)Full Signature