Preface
Vue 3 introduces the Composition API, which provides a more flexible and more organized way to manage the state of components. ref and reactive are two core functions used in the Composition API to declare reactive data. In Vue 3, using setup syntax sugar makes it easier to use these features. This article will explore how to use ref and reactive to manage state and explain the differences between them.
Basic usage of ref and reactive
Use ref
ref is used to create a responsive reference object, usually used for basic data types (such as strings, numbers, etc.).
<script setup> import { ref } from 'vue'; const count = ref(0); function increment() { ++; } </script> <template> <button @click="increment">Count is: {{ count }}</button> </template>
In this example, we create a name calledcount
Responsive references and throughincrement
Function to update its value. Pay attention, access and modifyref
When creating responsive data, you need to pass.value
Attributes.
Use reactive
reactive
Used to create a responsive complex data type, such as an object or an array.
<script setup> import { reactive } from 'vue'; const state = reactive({ count: 0, message: 'Hello Vue 3!' }); function increment() { ++; } </script> <template> <button @click="increment">Count is: {{ }}</button> <p>{{ }}</p> </template>
In this example, we create a name calledstate
Responsive object, which contains two properties:count
andmessage
. passincrement
Function updatecount
The value of the attribute.
The difference between ref and reactive
1. Data Type
-
ref
Used for basic data types (such as strings, numbers, boolean values, etc.). -
reactive
Used for complex data types (such as objects, arrays, etc.).
2. Access and modify
-
ref
The responsive reference created needs to be passed.value
Properties to access and modify. -
reactive
The responsive object created can directly access and modify its properties without using.value
。
3. Purpose
- When you need to use a basic data type as responsive data, use
ref
。 - When you need to use an object or array as responsive data, use
reactive
。
4. Combined with toRefs
When you need toreactive
When each property of an object is referenced as an independent responsive, you can usetoRefs
。
<script setup> import { reactive, toRefs } from 'vue'; const state = reactive({ count: 0, message: 'Hello Vue 3!' }); const { count, message } = toRefs(state); function increment() { ++; } </script> <template> <button @click="increment">Count is: {{ count }}</button> <p>{{ message }}</p> </template>
In this example, we usetoRefs
Willreactive
Each property of the object is converted into an independent responsive reference, so that it can be used asref
Operate them the same.
Summarize
Vue 3's Composition API provides two ways to manage state, ref and reactive, each with its own characteristics and applicable scenarios. ref is suitable for basic data types, while reactive is suitable for complex data types. Understanding the difference between them and using them correctly can help you manage the state of components more effectively and write clearer and maintainable code.
This is the article about Vue3's code examples for managing state using ref and reactive. For more information about Vue3's status management using ref and reactive, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!