Skip to content

Instantly share code, notes, and snippets.

@antlis
Created April 16, 2026 20:17
Show Gist options
  • Select an option

  • Save antlis/8baab6141f2bb785bb34da7bcc830d45 to your computer and use it in GitHub Desktop.

Select an option

Save antlis/8baab6141f2bb785bb34da7bcc830d45 to your computer and use it in GitHub Desktop.
Reactive debounced ref for handling user input (search, filters) without excessive API calls.
export function useDebouncedRef<T>(value: T, delay = 300) {
const state = ref(value)
const debounced = ref(value)
let timer: any
watch(state, (val) => {
clearTimeout(timer)
timer = setTimeout(() => {
debounced.value = val
}, delay)
})
return { state, debounced }
}
@antlis
Copy link
Copy Markdown
Author

antlis commented Apr 16, 2026

Use case:

const { state: search, debounced } = useDebouncedRef('', 300)

watch(debounced, (q) => {
  // fire API request
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment