use-counter
- A simple hook for managing counter.
Parameters
Parameter | Type | Required | Default Value | Description |
---|---|---|---|---|
key | string | ❌ | "" | Based on the key, it generates type-safe object with prefixed proprety. |
initialValue | number | ❌ | 0 | Initial value of the counter. |
Returns
- It returns an object.
counter
: numberincrementCounter
: () => voiddecrementCounter
: () => void
Usage
ts
import { useCounter } from 'classic-react-hooks'
export default function YourComponent() {
const { counter, decrementCounter, incrementCounter } = useCounter()
// If key is passed then properties within the object is prefixed with it.
// const { userCounter, incrementUserCounter, decrementUserCounter } = useCounter("user")
return (
<div>
<div>
<button onClick={decrementCounter}>decrement</button>
<p>{counter}</p>
<button onClick={incrementCounter}>increment</button>
</div>
</div>
)
}