|
| 1 | +# useCounter |
| 2 | + |
| 3 | +`useCounter` is a React hook that manages a numeric counter state with increment, decrement, and reset capabilities. Optionally, you can provide minimum and maximum values to constrain the counter's range. |
| 4 | + |
| 5 | +## Interface |
| 6 | + |
| 7 | +```ts |
| 8 | +function useCounter(options?: UseCounterOptions): UseCounterReturn; |
| 9 | + |
| 10 | +type UseCounterOptions = { |
| 11 | + initialValue?: number; |
| 12 | + min?: number; |
| 13 | + max?: number; |
| 14 | + step?: number; |
| 15 | +}; |
| 16 | + |
| 17 | +type UseCounterReturn = { |
| 18 | + count: number; |
| 19 | + increment: () => void; |
| 20 | + decrement: () => void; |
| 21 | + reset: () => void; |
| 22 | + setCount: (value: number | ((prev: number) => number)) => void; |
| 23 | +}; |
| 24 | +``` |
| 25 | + |
| 26 | +### Parameters |
| 27 | + |
| 28 | +<Interface |
| 29 | + name="options" |
| 30 | + type="UseCounterOptions" |
| 31 | + description="Optional configuration for the counter." |
| 32 | + :nested="[ |
| 33 | + { |
| 34 | + name: 'initialValue', |
| 35 | + type: 'number', |
| 36 | + description: 'Initial value for the counter. Defaults to 0.', |
| 37 | + }, |
| 38 | + { |
| 39 | + name: 'min', |
| 40 | + type: 'number', |
| 41 | + description: 'Minimum value the counter can reach. If not provided, there is no lower limit.', |
| 42 | + }, |
| 43 | + { |
| 44 | + name: 'max', |
| 45 | + type: 'number', |
| 46 | + description: 'Maximum value the counter can reach. If not provided, there is no upper limit.', |
| 47 | + }, |
| 48 | + { |
| 49 | + name: 'step', |
| 50 | + type: 'number', |
| 51 | + description: 'Value to increment or decrement by. Defaults to 1.', |
| 52 | + } |
| 53 | + ]" |
| 54 | +/> |
| 55 | + |
| 56 | +### Return Value |
| 57 | + |
| 58 | +<Interface |
| 59 | + name="" |
| 60 | + type="UseCounterReturn" |
| 61 | + description="An object with count value and control functions." |
| 62 | + :nested="[ |
| 63 | + { |
| 64 | + name: 'count', |
| 65 | + type: 'number', |
| 66 | + description: 'Current count value.', |
| 67 | + }, |
| 68 | + { |
| 69 | + name: 'increment', |
| 70 | + type: '() => void', |
| 71 | + description: 'Increment the counter by the step amount.', |
| 72 | + }, |
| 73 | + { |
| 74 | + name: 'decrement', |
| 75 | + type: '() => void', |
| 76 | + description: 'Decrement the counter by the step amount.', |
| 77 | + }, |
| 78 | + { |
| 79 | + name: 'reset', |
| 80 | + type: '() => void', |
| 81 | + description: 'Reset the counter to its initial value.', |
| 82 | + }, |
| 83 | + { |
| 84 | + name: 'setCount', |
| 85 | + type: '(value: number | ((prev: number) => number)) => void', |
| 86 | + description: 'Set the counter to a specific value within constraints.', |
| 87 | + } |
| 88 | + ]" |
| 89 | +/> |
| 90 | + |
| 91 | +## Example |
| 92 | + |
| 93 | +```tsx |
| 94 | +import { useCounter } from 'react-simplikit'; |
| 95 | + |
| 96 | +function ShoppingCart() { |
| 97 | + const { count, increment, decrement, reset } = useCounter({ |
| 98 | + initialValue: 1, |
| 99 | + min: 1, |
| 100 | + max: 10, |
| 101 | + }); |
| 102 | + |
| 103 | + return ( |
| 104 | + <div> |
| 105 | + <span>Quantity: {count}</span> |
| 106 | + <button type="button" onClick={decrement}>-</button> |
| 107 | + <button type="button" onClick={increment}>+</button> |
| 108 | + <button type="button" onClick={reset}>Reset</button> |
| 109 | + </div> |
| 110 | + ); |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Constraints |
| 115 | + |
| 116 | +The hook automatically ensures that the counter stays within the specified bounds: |
| 117 | + |
| 118 | +- When incrementing beyond `max`, the value will stay at `max` |
| 119 | +- When decrementing below `min`, the value will stay at `min` |
| 120 | +- When using `setCount`, any value outside the bounds will be adjusted to the nearest boundary |
0 commit comments