Home >Backend Development >PHP Tutorial >Vue.js da (ref va reactive) farqi
When choosing ref and reactive hooks in Vue.js, it is necessary to understand their differences and in what situations to use them. Both hooks are used to create reactive data, but the way they work and their usage is slightly different.
Suitable for primitive values: ref is mainly useful for primitive types (string, number, boolean). For example, for simple values like count, message.
Referencing DOM Elements: ref is used to store and refer to DOM elements. For example,
.Accessing the value is easy: when working with ref, the value can be accessed and changed via .value.
import { ref } from 'vue'; const count = ref(0); count.value++; // Qiymatni oshirish
import { reactive } from 'vue'; const state = reactive({ count: 0, name: 'Vue' }); state.count++; // Qiymatni oshirish state.name = 'Vue 3'; // Xususiyatni o'zgartirish
Value Type:
Use cases:
Reactivity:
Here is an example of using ref and reactive together:
<template> <div> <p>Message: {{ message }}</p> <p>Todos:</p> <ul> <li v-for="todo in todos" :key="todo.id">{{ todo.text }}</li> </ul> <input v-model="newTodoText" placeholder="New todo" /> <button @click="addTodo">Add Todo</button> </div> </template> <script setup> import { ref, reactive } from 'vue'; const message = ref('Hello, Vue 3!'); const todos = reactive([ { id: 1, text: 'Learn Vue 3' }, { id: 2, text: 'Build something awesome' } ]); const newTodoText = ref(''); function addTodo() { todos.push({ id: todos.length + 1, text: newTodoText.value }); newTodoText.value = ''; } </script>
This example shows how ref and reactive hooks can be used together. The choice depends on what type of data you are working with.
PS: Why does it say that in the picture above, ?????????? , I will answer this question in the video lesson :)
You can follow us on networks and if the article is useful, comment and share it with your Vuechi friends. ?
The above is the detailed content of Vue.js da (ref va reactive) farqi. For more information, please follow other related articles on the PHP Chinese website!