The setup function is where you prepare your component's data and logic in Vue 3. It helps organize your code clearly and makes it easy to use Vue's new features.
Setup function basics in Vue
<script setup> import { ref } from 'vue' // Define reactive data const count = ref(0) // Define a function function increment() { count.value++ } </script> <template> <button @click="increment">Count is: {{ count }}</button> </template>
The <script setup> block is a special Vue 3 syntax that runs the setup function automatically.
Use ref() to create reactive variables that update the UI when changed.
<script setup> import { ref } from 'vue' const message = ref('Hello!') </script> <template> <p>{{ message }}</p> </template>
<script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value++ } </script> <template> <button @click="increment">Clicked {{ count }} times</button> </template>
reactive to track an object and update age on button click.<script setup> import { reactive } from 'vue' const user = reactive({ name: 'Anna', age: 25 }) function birthday() { user.age++ } </script> <template> <p>{{ user.name }} is {{ user.age }} years old.</p> <button @click="birthday">Happy Birthday!</button> </template>
This component shows a button with a count. Each click increases the count number. The ref makes the count reactive, so the UI updates automatically.
It also includes an aria-label for accessibility, helping screen readers describe the button.
<script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value++ } </script> <template> <button @click="increment" aria-label="Increment count">Count is: {{ count }}</button> </template>
Always import ref or reactive from 'vue' to create reactive data.
Use value to access or change the value inside a ref.
The <script setup> block is simpler and recommended for new Vue 3 components.
The setup function is where you define reactive data and functions in Vue 3.
Use ref for simple reactive variables and reactive for objects.
<script setup> makes writing setup code easier and cleaner.
Practice
setup function in a Vue 3 component?Solution
Step 1: Understand the role of
Thesetupin Vue 3setupfunction is where you declare reactive data and functions that the component will use.Step 2: Compare with other options
Styling, routing, and global registration are handled elsewhere, not insetup.Final Answer:
To define reactive data and functions before the component renders -> Option BQuick Check:
Setup function = reactive data + functions [OK]
- Confusing setup with styling or routing
- Thinking setup runs after rendering
- Assuming setup registers global components
setup function using Vue 3 Composition API?Solution
Step 1: Identify correct reactive declaration for simple values
For simple reactive variables like numbers,refis used, e.g.,const count = ref(0);.Step 2: Check other options
reactiveis for objects, not primitives;useStateis not a Vue function.Final Answer:
const count = ref(0); -> Option DQuick Check:
Simple reactive variable = ref() [OK]
- Using reactive() for primitive values
- Trying to use non-existent useState()
- Wrapping primitive in reactive({ value: ... }) unnecessarily
import { ref } from 'vue';
export default {
setup() {
const message = ref('Hello');
function update() {
message.value = 'Hi';
}
return { message, update };
}
}What will be the value of
message after calling update()?Solution
Step 1: Understand ref and updating its value
messageis a ref holding 'Hello'. Callingupdate()setsmessage.valueto 'Hi'.Step 2: Determine final value after update
Afterupdate(),message.valueis 'Hi', somessagereactive variable reflects 'Hi'.Final Answer:
'Hi' -> Option AQuick Check:
Ref value updated correctly = 'Hi' [OK]
- Forgetting to use .value to update ref
- Assuming message itself changes, not message.value
- Thinking update() does not affect message
setup function:import { ref } from 'vue';
export default {
setup() {
const count = ref(0);
function increment() {
count = count + 1;
}
return { count, increment };
}
}Solution
Step 1: Check how ref variables are updated
Ref variables hold their value inside.value. Direct assignment likecount = count + 1is invalid.Step 2: Correct way to update ref
Usecount.value = count.value + 1orcount.value++to update.Final Answer:
Cannot assign to ref variable directly; should use count.value -> Option CQuick Check:
Update ref with .value [OK]
- Assigning directly to ref variable
- Forgetting to use .value when updating
- Confusing reactive and ref update syntax
<script setup> that has a reactive object with properties name and age. Which code correctly defines this reactive object and updates age by 1 inside setup?Solution
Step 1: Choose correct reactive type for object
Usereactivefor objects to make all properties reactive.Step 2: Update property correctly
Withreactive, update properties directly likeperson.age++. Usingrefwith objects requires accessing.value.Final Answer:
import { reactive } from 'vue'; const person = reactive({ name: 'Alice', age: 30 }); person.age++; -> Option AQuick Check:
Reactive object updated directly =import { reactive } from 'vue'; const person = reactive({ name: 'Alice', age: 30 }); person.age++;[OK]
- Using ref for objects without .value access
- Reassigning reactive object instead of updating property
- Trying to increment property on ref object without .value
Ready to prove it?
Pass the test and this pattern moves unknown
