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