Thuta Learning
Vue.js
IntermediateWeb DevelopmentBeginner

Reusable Logic with Composables

Vue.jsLesson 1520 min

What you'll walk away with

  • Build a composable
  • Return reactive state and functions
  • Add cleanup

Explain it simply

Reusable Logic with Composables, in plain language

A composable is a function that packages up stateful logic using the Vue Composition API, and it's usually named starting with use. It should return state, computed values, and actions — no UI markup.

Problem → Why → Solution

The problem — why this concept matters

Calling a composable outside a component, in some random scope

A common mistake

Unwrapping a reactive value into a plain value and losing reactivity

Core concept — understand how it works

Extract Composition API logic into use... functions and reuse it across components.

Mental model
  1. reactive source
  2. dependency tracking
  3. updated value or UI

Build a composable

Core syntax or pattern

javascript
// useCounter.js
import { ref, computed } from 'vue'

export function useCounter(initial = 0) {
  const count = ref(initial)
  const doubled = computed(() => count.value * 2)
  const increment = () => count.value++
  return { count, doubled, increment }
}

// component
const { count, doubled, increment } = useCounter(5)

Extract Composition API logic into use... functions and reuse it across components.

Read the code step by step

  1. reactive source// useCounter.js

    A composable is a function that packages up stateful logic using the Vue Composition API, and it's usually named starting with use. It should return state, computed values, and actions — no UI markup.

  2. dependency trackingconst increment = () => count.value++

    Extract Composition API logic into use... functions and reuse it across components.

  3. updated value or UIconst { count, doubled, increment } = useCounter(5)

    Apply Reusable Logic with Composables in a small example and test normal, edge, and failure cases.

Execution flow
  1. reactive source
  2. dependency tracking
  3. updated value or UI

Success

Build a composable

Failure

Calling a composable outside a component, in some random scope

Let's break it down simply

A composable is a function that packages up stateful logic using the Vue Composition API, and it's usually named starting with use. It should return state, computed values, and actions — no UI markup.

javascript
// useCounter.js
import { ref, computed } from 'vue'

export function useCounter(initial = 0) {
  const count = ref(initial)
  const doubled = computed(() => count.value * 2)
  const increment = () => count.value++
  return { count, doubled, increment }
}

// component
const { count, doubled, increment } = useCounter(5)
You should see
count = 5
doubled = 10

Try it yourself

Write a useFetch composable that returns loading, data, error, and an execute function.

Composables — Vue.js

Real-world usage

How to apply it in a real project

Apply Reusable Logic with Composables in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Write a useFetch composable that returns loading, data, error, and an execute function.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
javascript
// useCounter.js
import { ref, computed } from 'vue'

export function useCounter(initial = 0) {
  const count = ref(initial)
  const doubled = computed(() => count.value * 2)
  const increment = () => count.value++
  return { count, doubled, increment }
}

// component
const { count, doubled, increment } = useCounter(5)

Expected: Build a composable

Show hint

Apply Reusable Logic with Composables in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of Reusable Logic with Composables?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • reactive source → dependency tracking → updated value or UI
  • Extract Composition API logic into use... functions and reuse it across components.
  • Calling a composable outside a component, in some random scope
  • Apply Reusable Logic with Composables in a small example and test normal, edge, and failure cases.

Composables — Vue.js

Easy traps

  • Calling a composable outside a component, in some random scope
  • Unwrapping a reactive value into a plain value and losing reactivity

Exercise

Write a useFetch composable that returns loading, data, error, and an execute function.

You'll know it worked when: count = 5 doubled = 10

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Reusable Logic with Composables | Thuta Learning