Skip to content

Composable

A composable is a function that leverages Vue's Composition API to encapsulate and reuse stateful logic.

ts
// mouse.ts
import { ref, onMounted, onUnmounted } from 'vue'

// by convention, composable function names start with "use"
export function useMouse() {
  // state encapsulated and managed by the composable
  const x = ref(0)
  const y = ref(0)

  // a composable can update its managed state over time.
  function update(event) {
    x.value = event.pageX
    y.value = event.pageY
  }

  // a composable can also hook into its owner component's
  // lifecycle to setup and teardown side effects.
  onMounted(() => window.addEventListener('mousemove', update))
  onUnmounted(() => window.removeEventListener('mousemove', update))

  // expose managed state as return value
  return { x, y }
}

Convention & Best Practices

Naming

It is a convention to name composable functions with camelCase names that start with use.

Example: useMouse()

Input Arguments

ts
import { toValue } from 'vue'

function useFeature(maybeRefOrGetter) {
  // If maybeRefOrGetter is a ref or a getter,
  // its normalized value will be returned.
  // Otherwise, it is returned as-is.
  const value = toValue(maybeRefOrGetter)
}

Return values

ts
// x and y are refs
const { x, y } = useMouse()

Subscribe to our newsletter

A weekly latest Articles, UI Kits, Free & Premium Resources.