0Pricing
Vue Academy · Lesson

Composable Factories and Parameterized Composables

Creating composable factories, scoping state per instance, avoiding shared singleton state.

Composables That Take Parameters

A plain composable returns fixed behavior. A parameterized composable accepts arguments to customize what it does — a base URL, options, defaults — making one function serve many cases.

A Simple Parameterized Composable

Pass configuration as arguments. Here the URL is provided by the caller, so one useFetch works for any endpoint.

import { ref } from "vue"

export function useFetch(url) {
  const data = ref(null)
  fetch(url).then(r => r.json()).then(d => data.value = d)
  return { data }
}

All lessons in this course

  1. Advanced Reactivity: shallowRef and triggerRef
  2. Composable Factories and Parameterized Composables
  3. State Machines with XState in Vue
  4. effectScope for Grouped Effects
← Back to Vue Academy