Skip to Content
ohooks

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

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useCounter.json

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
max?
number
Maximum allowed counter value

UseCounterReturn

Counter value and helper methods returned by the hook.

Property
Specification
count
number
Current counter value
inc
functiondefault:1
Increments the counter by delta
dec
functiondefault:1
Decrements the counter by delta
set
function
Sets the counter to a specific value or updater function (clamped to min/max)
reset
function
Resets the counter back to initialValue (clamped to min/max)