Vue.js / Vue Composition API
Using reactive() and ref() for State
In this tutorial, you will learn how to use reactive() and ref(), two essential functions provided by the Vue Composition API, to create reactive data sources.
Section overview
5 resourcesExplores Vue 3's Composition API for structuring applications.
Introduction
In this tutorial, we will learn about using reactive() and ref() in Vue.js to create reactive data sources. These are key functions provided by Vue's Composition API, which is a set of additive APIs that allows flexible composition of component logic.
By the end of this tutorial, you will understand:
- What reactive() and ref() are
- How to use reactive() and ref()
- Differences between reactive() and ref()
Prerequisites
- Familiarity with JavaScript
- Basic knowledge of Vue.js
Step-by-Step Guide
Understanding reactive()
reactive() function is used to create a reactive object. The reactive conversion is "deep": it affects all nested properties. In other words, if any property in the reactive object changes, Vue is aware of it and can trigger updates in the UI.
import { reactive } from 'vue'
const state = reactive({
count: 0
})
state.count++ // Vue is aware of this change
Understanding ref()
ref() creates a reactive reference. Unlike reactive(), the reactive conversion is "shallow". If you use ref() with an object, Vue will not track changes in the object's properties.
import { ref } from 'vue'
const count = ref(0)
count.value++ // Vue is aware of this change
Code Examples
Using reactive()
import { reactive } from 'vue'
const state = reactive({
count: 0,
message: 'Hello, Vue!'
})
// Update a property
state.count++
console.log(state.count) // Expected output: 1
// Add a new property
state.newMessage = 'Hello, reactive!'
console.log(state.newMessage) // Expected output: 'Hello, reactive!'
Using ref()
import { ref } from 'vue'
const count = ref(0)
console.log(count.value) // Expected output: 0
// Update the value
count.value++
console.log(count.value) // Expected output: 1
Summary
In this tutorial, we learned about using reactive() and ref(), two essential functions provided by the Vue Composition API. reactive() is used to make an object reactive, while ref() is used to create a reactive reference.
Practice Exercises
- Exercise: Create a reactive object with
reactive()that includes anameproperty and amessageproperty. Update both properties and log them to the console.
Solution:
```javascript
import { reactive } from 'vue'
const state = reactive({
name: 'Vue',
message: 'Hello, Vue!'
})
state.name = 'Vue.js'
state.message = 'Hello, Vue.js!'
console.log(state.name) // Expected output: 'Vue.js'
console.log(state.message) // Expected output: 'Hello, Vue.js!'
```
- Exercise: Create a reactive reference with
ref(). Update the reference's value and log it to the console.
Solution:
```javascript
import { ref } from 'vue'
const count = ref(0)
count.value++
console.log(count.value) // Expected output: 1
```
For further practice, try creating more complex reactive objects and references. Try nesting reactive objects and references within each other and observe how Vue tracks changes.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article