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
- Advanced Reactivity: shallowRef and triggerRef
- Composable Factories and Parameterized Composables
- State Machines with XState in Vue
- effectScope for Grouped Effects