Skip to content

refDebounced

Category
Export Size
451 B
Last Changed
4 months ago
Alias
useDebouncedebouncedRef
Related

Debounce execution of a ref value.

Demo

Delay is set to 1000ms for this demo.

Debounced:

Times Updated: 0

Usage

js
import { refDebounced } from '@vueuse/core'
import { shallowRef } from 'vue'

const input = shallowRef('foo')
const debounced = refDebounced(input, 1000)

input.value = 'bar'
console.log(debounced.value) // 'foo'

await sleep(1100)

console.log(debounced.value) // 'bar'

An example with object ref.

js
import { refDebounced } from '@vueuse/core'
import { shallowRef } from 'vue'

const data = shallowRef({
  name: 'foo',
  age: 18,
})
const debounced = refDebounced(data, 1000)

function update() {
  data.value = {
    ...data.value,
    name: 'bar',
  }
}

console.log(debounced.value) // { name: 'foo', age: 18 }
update()
await sleep(1100)

console.log(debounced.value) // { name: 'bar', age: 18 }

You can also pass an optional 3rd parameter including maxWait option. See useDebounceFn for details.

Type Declarations

typescript
export type RefDebouncedReturn<T = any> = Readonly<Ref<T>>
/**
 * Debounce updates of a ref.
 *
 * @return A new debounced ref.
 */
export declare function refDebounced<T>(
  value: Ref<T>,
  ms?: MaybeRefOrGetter<number>,
  options?: DebounceFilterOptions,
): RefDebouncedReturn<T>
export { refDebounced as debouncedRef, refDebounced as useDebounce }

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
Anthony Fu
IlyaL
Robin
Thimo Sietsma
Dominik Klein

Changelog

v13.1.0 on
c1d6e - feat(shared): ensure return types exists (#4659)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter

Released under the MIT License.

AI-Driven Development
You. But 10x faster
The structured masterclass to help you leverage AI and build with an edge
Join Early