Independent Composable Calls
Part of the Fundamentals section of Coddy's Vue journey. Lesson 49 of 55.
Calling a composable twice can create two independent state groups. The ref must be created inside the function so each call receives its own container.
function useStep(initial) {
const step = ref(initial);
const next = () => step.value++;
return { step, next };
}
const first = useStep(1);
const second = useStep(9);A ref declared at module scope would instead be shared by callers of that module. Both patterns can be deliberate, but this lesson asks for per-call state. Use destructuring aliases when a component needs two refs or actions with the same exported names.
Challenge
MediumCreate useCounter(initial) with its count ref inside the function and a returned add() action. Call it twice in the app, with 2 and 8. Show the left count in #result and right count in #right. Wire #left-add and #right-add to their own actions; each call must be independent.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
7Reusable Components
Importing ComponentsIndependent InstancesPassing PropsProp Types and DefaultsRecap: Component Cards10Reusable Composables
Extracting Reactive LogicReturning Live RefsDerived Composable ValuesIndependent Composable CallsRecap: Donation Logic2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks