Skip to content

useCounter ​

Category
Export Size
185 B
Last Changed
last month

Basic counter with utility functions.

Demo ​

Count: 0

Basic Usage ​

js
import { useCounter } from '@vueuse/core'

const { count, inc, dec, set, reset } = useCounter()

Usage with options ​

js
import { useCounter } from '@vueuse/core'

const { count, inc, dec, set, reset } = useCounter(1, { min: 0, max: 16 })

Type Declarations ​

typescript
export interface UseCounterOptions {
  min?: number
  max?: number
}
/**
 * Basic counter with utility functions.
 *
 * @see https://vueuse.org/useCounter
 * @param [initialValue]
 * @param options
 */
export declare function useCounter(
  initialValue?: MaybeRef<number>,
  options?: UseCounterOptions,
): {
  count: Ref<number, MaybeRef<number>>
  inc: (delta?: number) => number
  dec: (delta?: number) => number
  get: () => number
  set: (val: number) => number
  reset: (val?: number) => number
}

Source ​

Source • Demo • Docs

Contributors ​

Anthony Fu
Anthony Fu
Tycho
leewakin
Rock070
iChengbo
Damien Roche
Alex Kozack

Changelog ​

v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.8.0 on 2/20/2024
cf29c - fix: allow delta to be negative (#3650)
v10.3.0 on 7/30/2023
6ae3a - feat: initialValue support ref (#3266)
v9.4.0 on 10/25/2022
cb78f - fix: set and reset should also be limited (#2179)

Released under the MIT License.