useCounter
Manage a counter value with custom min, max and step properties
The useCounter
hook is helpful when you want to manage a counter value with custom min, max and step properties.
The hook returns the latest value of counter and provide handlers to perform increment
, decrement
, set
and reset
operations.
useCounter
Counter Value: 0
Usage
Import the hook from @abhushanaj/react-hooks
and use in required component.
With config options
Properties
-
The initialCount cannot be more than
max
property. It throws an error with messageInitial count cannot be less than min set from options.min
. -
The initialCount cannot be less than
min
property. It throws an error with messageInitial count cannot be more than max set from options.max
. -
The set method throw an error, if the value passed overflows the
options.max
andoptions.min
value.
API Reference
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
initialCount | number | The initial value of the counter. | N/A |
options | UseCounterOptions (optional) | The configuration options for the counter | undefined |
Return Value
The hook returns a tuple with [count, countUpdateFn]
options.
Parameter | Type | Description | Default |
---|---|---|---|
count | number | The current count value. | N/A |
countUpdateFn | CountUpdateFn | The updater functions to increment, decrement, set and reset the counter value. | N/A |
Types Reference
1. UseCounterOptions
The config options for the counter
Parameter | Type | Description | Default |
---|---|---|---|
step | number (optional) | The step value to incremtnt or decrement with. | 1 |
min | number (optional) | The minimum value the counter can reach. | -Infinity |
max | number (optional) | The max value the counter can reach. | Infinity |
2. CountUpdateFn
The handlers to update for the counter as an object
Parameter | Type | Description | Default |
---|---|---|---|
increment | ()=>void | To increment the counter with step value. | N/A |
decrement | ()=>void | To decrement the counter with step value. | N/A |
reset | ()=>void | To reset the counter to initialCount value. | N/A |
set | (newCount: number)=>void | To set the counter to newCount value. | N/A |