Skip to content
Learn Netverks

Lesson

Step 22/36 61% through track

composables

Composables

Last reviewed Jun 1, 2026 Content v20260601
Track mode
client_vue
Means
In-browser Vue TS
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches Composables: the concepts, APIs, and habits you need before advancing in Vue.

Composables extract reusable setup logic—the Vue equivalent of custom hooks.

You will apply Composables in contexts like: Greenfield SPAs, dashboards, design systems, and full-stack apps that pair Vue with PHP or Node APIs.

Write TypeScript, click Run—Vue 3 loads from CDN with the template compiler, mountApp shows UI in #app, and printOutput feeds the terminal.

When you can explain the previous lesson's ideas without copying starter code.

A composable is a function that encapsulates reactive state and logic for reuse—Vue’s equivalent of React custom hooks. Name composables useSomething by convention.

Example shape

function useCounter(initial = 0) {
  const count = ref(initial);
  function increment() { count.value += 1; }
  return { count, increment };
}

Each call creates independent refs—call useCounter() twice for two unrelated counters, just like calling a custom hook twice in React.

What belongs in composables

  • Shared form field logic, pagination, fetch + loading/error state
  • DOM listeners with automatic cleanup via lifecycle hooks inside the composable
  • Domain rules that multiple components need

Testing benefit

Composables are plain functions—test them without mounting components by checking returned refs and calling exposed methods.

Self-check

  1. Why prefix composables with use?
  2. Do two components sharing a composable share the same ref automatically?

Challenge

useToggle composable

  1. Extract useToggle(initial) returning open and toggle.
  2. Use it for two independent panels in App.

Done when: each panel toggles independently using the same composable logic.

Challenge

Extract useCounter

  1. Move increment logic into function useCounter(initial).
  2. Use two counters in the same parent.

Done when: both counters work independently from shared composable logic.

Interview prep

What is a composable?

A reusable function useX() that encapsulates reactive state and logic—like custom hooks in React, shared across components.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Composable you would extract?
  • Naming convention?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump