Menu
CoddyTech

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 icon

Challenge

Medium

Create 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>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals