Vue3: how to declare reactive states

vue3:-how-to-declare-reactive-states

Ler em pt/br

In a Single Page Application (SPA), reactivity is the ability of the application to dynamically respond to changes in its data, updating parts of the page without the need to reload it from scratch.

When Vue detects changes in its states, it utilizes the virtual DOM to re-render the interface and ensure that it is always up-to-date, making the process as fast and efficient as possible.

There are different ways to declare reactive states in a Vue application, which may vary depending on the API in use (Options or Composition), as well as the data types themselves. That’s what we’ll cover in this article!

Table of Contents
Reactive data with Options API:
Data properties
Computed Properties
Difference between methods and computed properties
Reactive data with Composition API:
ref()
reactive()
reactive() limitations
computed()
Wrapping it up…

Chemical reaction

Reactive data with Options API

Data properties

With the Options API, we declare reactive states of a component through data properties, using the data method for that purpose. This method returns an object with all reactive states and their values.

<script>
export default {
  data () {
    counter: 0
  }
}
script>

In the example above, counter is a reactive data, and if modified, the interface will automatically update to reflect the changes.

To see reactivity in action, let’s create a method (or function) that modifies the value of counter each time we click a button.

<script>
export default {
  data () {
    counter: 0
  },
  methods: {
    increaseCounter() {
      this.counter++
    }
  }
}
script>

Counter updating

Note that, upon clicking the button, the value of counter is automatically updated in the interface.

It’s important for all reactive data to be used in the component to be declared in the data option, even if they don’t have a defined value yet, as they will be instantiated in the component as soon as it is created (allowing the use of this in methods and lifecycle hooks).

If you need to initialize a state without a defined value, you can use undefined, null, or any other value that serves as a placeholder (such as an empty string, for example).

Computed Properties

Computed properties are used when we need to perform calculations or other logic based on data properties in a passive manner. In other words, when a reactive data is changed, all computed properties that depend on that data are also updated. It’s as if they were formatting functions for variables.

To declare computed properties, we use the function syntax within the computed option. This function should always return a value.

<script>
  export default {
    // hidden code
    computed: {
      doubleCounter() {
        return this.counter * 2
      }
    }
  }
script>

Note that we did not use the arrow function syntax in our computed property because they do not have this in their context, preventing us from accessing our data properties.

The computed property above always returns twice the value of counter. We can see in our application that whenever counter is modified, the value of doubleCounter is automatically updated:

doubleCounter updating

The same result could be achieved directly in the template of our component by inserting the logic within the interpolation ({{ }}, also called “mustache”).

<template>
  

The double of counter is: {{ doubleCounter }}

The double of counter is: {{ counter * 2 }}