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.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explores 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

  1. Exercise: Create a reactive object with reactive() that includes a name property and a message property. 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!'
```

  1. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

Image Compressor

Reduce image file sizes while maintaining quality.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Unit Converter

Convert between different measurement units.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help