Master Vue 3 Composition API reactivity patterns including ref, reactive, computed properties, and deep reactivity for building modern Vue applications.
This skill has safety concerns that you should review before use. Some patterns were detected that may pose a risk.Safety score: 60/100.
KillerSkills scans all public content for safety. Use caution before installing or executing flagged content.
Learn and implement Vue 3's Composition API reactivity system, focusing on `ref()`, `reactive()`, computed properties, and best practices for reactive state management.
This skill guides you through Vue 3's reactivity fundamentals from the official Vue.js documentation. You'll learn how to declare reactive state using `ref()` and `reactive()`, understand the differences between them, work with computed properties, and handle deep reactivity patterns in modern Vue applications.
When the user requests help with Vue 3 reactivity, follow these steps:
First, determine what aspect of Vue 3 reactivity the user needs help with:
Based on the user's needs, provide targeted guidance:
**For Declaring Reactive State with `ref()`:**
```javascript
import { ref } from 'vue'
const count = ref(0)
console.log(count.value) // 0
count.value++
console.log(count.value) // 1
```
**For Using `<script setup>` Syntax:**
```vue
<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
count.value++
}
</script>
<template>
<button @click="increment">{{ count }}</button>
</template>
```
**For `reactive()` vs `ref()`:**
```javascript
// Use ref() for primitives
const count = ref(0)
// Use ref() for objects that might be replaced
const user = ref({ name: 'John' })
// Use reactive() for local object state that won't be replaced
const state = reactive({
count: 0,
items: []
})
```
**For Computed Properties:**
```javascript
import { ref, computed } from 'vue'
const count = ref(0)
const doubled = computed(() => count.value * 2)
console.log(doubled.value) // 0
count.value++
console.log(doubled.value) // 2
```
**For Deep Reactivity:**
```javascript
const obj = ref({
nested: { count: 0 },
arr: ['foo', 'bar']
})
// These mutations will be detected
obj.value.nested.count++
obj.value.arr.push('baz')
```
**For Ref Unwrapping Caveats:**
```javascript
const books = reactive([ref('Vue 3 Guide')])
// Need .value here
console.log(books[0].value)
const map = reactive(new Map([['count', ref(0)]]))
// Need .value here too
console.log(map.get('count').value)
```
When implementing reactivity patterns:
Always clarify these important points:
Recommend these patterns:
Help users avoid these issues:
**Example 1: Converting Options API to Composition API**
```javascript
// Before (Options API)
export default {
data() {
return { count: 0 }
},
computed: {
doubled() {
return this.count * 2
}
},
methods: {
increment() {
this.count++
}
}
}
// After (Composition API with <script setup>)
<script setup>
import { ref, computed } from 'vue'
const count = ref(0)
const doubled = computed(() => count.value * 2)
function increment() {
count.value++
}
</script>
```
**Example 2: Choosing Between ref() and reactive()**
```javascript
// Good: ref() for primitives and replaceable objects
const count = ref(0)
const user = ref({ name: 'John', age: 30 })
// Can replace entire object
user.value = { name: 'Jane', age: 25 }
// Avoid: reactive() loses reactivity when replaced
const state = reactive({ count: 0 })
// This breaks reactivity!
state = reactive({ count: 1 })
```
Use this skill when:
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/vue-3-reactivity-fundamentals/raw