Vue Composition API

ref, reactive, computed and setup().

✍️ 3 exercises + code challenge in this lesson

The Composition API organises logic by feature instead of by option type. It is the recommended style for new projects (in .vue files with <script setup>).

  • ref(0): a reactive value; read and write it with .value in JavaScript.
  • reactive({...}): a reactive object.
  • computed(() => ...), watch(), onMounted()

Key Takeaways: Vue Composition API

Mastering Vue Composition API helps you write clean and reliable Vue.js code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Vue Composition API Code Example

Vue.js
<script src="https://unpkg.com/vue@3.5.12/dist/vue.global.prod.js"></script>

<div id="app">
  <p>{{ count }} × 2 = {{ double }}</p>
  <button @click="count++">Increase</button>
</div>

<script>
  const { createApp, ref, computed } = Vue;
  createApp({
    setup() {
      const count = ref(1);
      const double = computed(() => count.value * 2);
      return { count, double };
    }
  }).mount("#app");
</script>
Free sign-in required · Google login available

Vue Composition API Practice Exercises

Check what you learned in “Vue Composition API”.

/ 3 solved
Fill in the blank

Create a reactive value with the Composition API.

const count = (0);
✓ Correct! Not quite. Try again.
Multiple choice

In JavaScript, how do you read a ref's value?

✓ Correct! Not quite. Try again.
Code challenge

Using setup(), create a ref name = "Vue 3" and return it so the <p> shows it.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services