Bird
Raised Fist0
Vueframework~10 mins

Why the Composition API exists in Vue - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why the Composition API exists
Start with Options API
↓
Face code organization issues
↓
Need better logic reuse
↓
Create Composition API
↓
Use setup() function to group logic
↓
Easier to share and maintain code
↓
Improved readability and flexibility
Shows the flow from using the Options API to the need for better code organization and logic reuse, leading to the creation of the Composition API.
Execution Sample
Vue
import { ref } from 'vue';

export default {
  setup() {
    const count = ref(0);
    function increment() {
      count.value++;
    }
    return { count, increment };
  }
}
A simple Vue component using the Composition API to manage a counter state and increment function.
Execution Table
StepActionState BeforeState AfterOutput/Effect
1Call setup()No reactive statecount = 0 (reactive)Reactive count created
2Define increment()count = 0Function increment definedFunction ready to update count
3Return count and incrementcount = 0count and increment exposedComponent can use count and increment
4User clicks buttoncount = 0count = 1UI updates to show count = 1
5User clicks button againcount = 1count = 2UI updates to show count = 2
💡 User stops clicking; reactive state remains updated and UI reflects latest count
Variable Tracker
VariableStartAfter 1After 2Final
count.value0122
Key Moments - 2 Insights
Why do we use setup() instead of data() and methods()?
setup() groups related logic together, making code easier to read and reuse, unlike data() and methods() which separate state and functions. See execution_table steps 1-3.
How does the Composition API help with code reuse?
By putting logic inside setup() and returning it, we can extract and share functions easily across components, unlike the Options API. This is shown in the way increment() is defined and returned in steps 2-3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of count after step 4?
A1
B0
C2
DUndefined
💡 Hint
Check the 'State After' column for step 4 in the execution_table.
At which step is the increment function created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column describing function definition in execution_table.
If we did not return count and increment from setup(), what would happen?
AThey would still be usable in the template
BThe component would crash
CThey would not be accessible in the template
DThe count would reset automatically
💡 Hint
Returning variables from setup() exposes them to the template, see step 3 in execution_table.
Concept Snapshot
Composition API groups related logic inside setup() function.
Use reactive refs like count = ref(0) to track state.
Return state and functions to expose them to the template.
Improves code reuse and readability over Options API.
Enables easier sharing of logic across components.
Full Transcript
The Composition API was created to solve problems with code organization and reuse in Vue's Options API. Instead of separating data and methods, the Composition API uses a setup() function to group related logic together. This makes code easier to read, maintain, and share. In the example, a reactive count variable is created with ref(0), and an increment function updates it. Both are returned from setup() so the template can use them. When the user clicks a button, increment updates count, and the UI reflects the new value. This flow shows how the Composition API improves flexibility and clarity in Vue components.

Practice

(1/5)
1. Why was the Composition API introduced in Vue?
easy
A. To remove support for reactive data
B. To replace templates with plain JavaScript
C. To organize component logic by grouping related code
D. To make Vue incompatible with TypeScript

Solution

  1. Step 1: Understand Vue component logic challenges

    Before Composition API, logic was scattered in options like data, methods, computed, making reuse and reading harder.
  2. Step 2: Identify Composition API purpose

    The Composition API groups related logic together, improving readability and reuse.
  3. Final Answer:

    To organize component logic by grouping related code -> Option C
  4. Quick Check:

    Composition API = Organize logic [OK]
Hint: Composition API groups related code for clarity [OK]
Common Mistakes:
  • Thinking it removes templates
  • Believing it disables reactivity
  • Assuming it breaks TypeScript support
2. Which of the following is the correct way to import the ref function from Vue's Composition API?
easy
A. import ref from 'vue';
B. import { ref } from 'vue';
C. const ref = require('vue');
D. import { Ref } from 'vue';

Solution

  1. Step 1: Recall ES module import syntax in Vue 3+

    Vue exports Composition API functions as named exports, so use curly braces for import.
  2. Step 2: Check each option

    import { ref } from 'vue'; uses named import correctly. import ref from 'vue'; tries default import which is wrong. const ref = require('vue'); uses CommonJS require which is outdated. import { Ref } from 'vue'; imports 'Ref' (capitalized) which is a type, not a function.
  3. Final Answer:

    import { ref } from 'vue'; -> Option B
  4. Quick Check:

    Named import with curly braces = import { ref } from 'vue'; [OK]
Hint: Use curly braces for named imports from Vue [OK]
Common Mistakes:
  • Using default import instead of named import
  • Using require() instead of import
  • Confusing types with functions
3. What will be the output of this Vue Composition API code snippet inside a component's setup()?
import { ref } from 'vue';
export default {
  setup() {
    const count = ref(0);
    count.value++;
    return { count };
  }
}
What is the value of count when rendered?
medium
A. 0
B. An error occurs
C. undefined
D. 1

Solution

  1. Step 1: Understand ref and its value property

    The ref(0) creates a reactive object with initial value 0 accessible via count.value.
  2. Step 2: Analyze the increment operation

    count.value++ increases the value from 0 to 1 before returning it.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    ref value incremented once = 1 [OK]
Hint: Remember to access .value with ref to get or set data [OK]
Common Mistakes:
  • Expecting count to be 0 without increment
  • Trying to use count directly without .value
  • Thinking ref returns a primitive, not an object
4. Identify the error in this Composition API code snippet:
import { reactive } from 'vue';
export default {
  setup() {
    const state = reactive({ count: 0 });
    function increment() {
      state.count += 1
    }
    return { state, increment };
  }
}
medium
A. No error; code works correctly
B. Reactive object properties should not be mutated directly
C. Reactive must be imported as ref
D. Missing semicolon after increment function

Solution

  1. Step 1: Review reactive usage

    The reactive function creates a reactive object whose properties can be mutated directly.
  2. Step 2: Check the increment function

    The function increments state.count correctly without syntax errors.
  3. Final Answer:

    No error; code works correctly -> Option A
  4. Quick Check:

    Reactive objects allow direct property mutation [OK]
Hint: Reactive objects allow direct property changes [OK]
Common Mistakes:
  • Thinking reactive properties are immutable
  • Confusing reactive with ref usage
  • Assuming missing semicolon causes error
5. You want to reuse a piece of logic that tracks a user's online status across multiple components. How does the Composition API help achieve this compared to the Options API?
hard
A. By allowing you to write a reusable function that returns reactive state and methods
B. By forcing you to duplicate the same data and methods in each component
C. By requiring you to use global variables for shared state
D. By removing support for reactive data in reusable code

Solution

  1. Step 1: Understand logic reuse challenges in Options API

    Options API scatters logic in component options, making reuse hard without mixins or plugins.
  2. Step 2: Recognize Composition API's reusable functions

    Composition API lets you write functions (composables) that return reactive state and methods to share logic easily.
  3. Final Answer:

    By allowing you to write a reusable function that returns reactive state and methods -> Option A
  4. Quick Check:

    Composition API enables reusable logic functions [OK]
Hint: Use composable functions to share reactive logic [OK]
Common Mistakes:
  • Thinking duplication is necessary
  • Believing global variables are required
  • Assuming reactive data can't be reused
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard