useCounter

Manage a counter value with minimum and maximum limits with useCounter.

Install:

npm i @uidotdev/usehooks

Description:

The useCounter hook is useful for managing a counter value with additional options for minimum and maximum limits. The hook takes a starting value and options object as parameters, which can specify minimum and maximum limits for the counter. If the starting value falls outside the specified limits, an error is thrown. The hook returns the current count value and an object containing functions to increment, decrement, set a specific count, and reset the counter.

Parameters

NameTypeDescription
startingValuenumberThe initial value for the counter. Default is 0.
optionsobjectAdditional options for the counter.
options.minnumberThe minimum value allowed for the counter.
options.maxnumberThe maximum value allowed for the counter.

Return Value

The useCounter hook returns an array with two elements:

NameParametersDescription
[0]The current value of the counter.
[1].incrementIncrements the counter by 1.
[1].decrementDecrements the counter by 1.
[1].setnextCount: numberSets the counter to the specified nextCount value.
[1].resetResets the counter to the initial startingValue.

Demo:

Example:

More Hooks:

Sort: