shallowRef and shallowReactive
shallowRef functions only process basic type data.
shallowReactive function only processes the first layer of data.
Both need to be introduced when using them.
Did you still not understand what was said above? It doesn’t matter, just remember the above three items first, and then explain them in detail.
We talked about the ref function and the reactive function in the previous blog. Their function is to convert the data into responsive data. When the data is modified, the data can be displayed on the page in real time, and the basic data can also be displayed on the page in real time. Okay, no matter who you are, it’s all like this.
But there is a problem. When we change the data to responsive data, whether we use the ref function or the reactive function, they are both deep monitoring. What does that mean? It is the object wrapped by reactive. Even if there are 100 layers, that is, if you click on a hundred attributes in a row, you can still monitor the deepest data. In this case, there will be problems.
Problems with deep monitoring:
Both the ref function and the reactive function are deep monitoring.
If the amount of data is too large, it will consume super performance.
If we don’t need to deeply monitor the data, we can use the shallowRef function and shallowReactive function.
do you understand? It doesn’t matter if you don’t understand, we will know through a few cases.
Use shallowReactive non-depth monitoring
Remember that the shallowReactive function can only process the first layer of data.
Suppose our page has a personal information display, with name and age to be displayed. Our data is stored in the boy object, and age is under the news attribute of the boy object, that is, deep, but name is in Below the boy object, that is, on the first layer, we have two buttons to modify the name and age respectively to see what the effect will be.
<template> <div> <h2 id="姓名-name">姓名:{{name}}</h2> <h2 id="年龄-news-age">年龄:{{news.age}}</h2> <button @click="btn">修改name</button> <button @click="btn2">修改age</button> </div> </template> <script> import { shallowReactive, toRefs } from "vue"; export default { name: "App", setup() { const boy = shallowReactive({ name: "我是????????.", news: { birthday: "2012-10-14", age: 10 } }); const btn = () => { boy.name = "????????."; }; const btn2 = () => { boy.news.age++; }; return { ...toRefs(boy), btn, btn2 }; } }; </script>
Let’s click two buttons respectively to see the page changes.
Through the effects, let’s summarize a little bit:
shallowReactive will only wrap the first layer of data
By default it can only listen to the first layer of data.
If you want to change the data of multiple layers, you must first change the data of the first layer, and then change the data of other layers. Only then will the data on the view change.
Use shallowRef for non-depth monitoring
As mentioned at the beginning, the shallowRef function can only process basic type data, why? Because The shallowRef function monitors .value changes. It is not a change in the first layer of data. So if you want to change the data created by shallowRef, you should xxx.value = XXX.
Look at the code:
<template> <div> <h2 id="姓名-boy">姓名:{{boy}}</h2> <button @click="btn">修改boy</button> </div> </template> <script> import { shallowRef } from "vue"; export default { name: "App", setup() { const boy = shallowRef("我是????????."); const btn = () => { boy.value = "????????."; }; return { boy, btn }; } }; </script>
Click the button to modify the value of boy.
As you can see from the screenshot above, the data can be modified normally.
Then a question remains: shallowRef function only processes basic type data?
Look at the following case:
<template> <div> <h2 id="姓名-boy-name">姓名:{{boy.name}}</h2> <h2 id="年龄-boy-news-age">年龄:{{boy.news.age}}</h2> <button @click="btn">修改name</button> <button @click="btn2">修改age</button> </div> </template> <script> import { shallowRef } from "vue"; export default { name: "App", setup() { const boy = shallowRef({ name: "我是????????.", news: { birthday: "2012-10-14", age: 10 } }); const btn = () => { boy.value.name = "????????."; }; const btn2 = () => { boy.value.news.age++; }; return { boy, btn, btn2 }; } }; </script>
In this code, we wrap an object with shallowRef, then display the name and age on the page, and then we modify the name and age through the button , take a look at the effect of the page.
So, the shallowRef function can only handle basic type data.
The above is the detailed content of How to use shallowRef and shallowReactive in Vue3. For more information, please follow other related articles on the PHP Chinese website!

前端有没有现成的库,可以直接用来绘制 Flowable 流程图的?下面本篇文章就跟小伙伴们介绍一下这两个可以绘制 Flowable 流程图的前端库。

vue不是前端css框架,而是前端JavaScript框架。Vue是一套用于构建用户界面的渐进式JS框架,是基于MVVM设计模式的前端框架,且专注于View层。Vue.js的优点:1、体积小;2、基于虚拟DOM,有更高的运行效率;3、双向数据绑定,让开发者不用再去操作DOM对象,把更多的精力投入到业务逻辑上;4、生态丰富、学习成本低。

Vue3如何更好地使用qrcodejs生成二维码并添加文字描述?下面本篇文章给大家介绍一下Vue3+qrcodejs生成二维码并添加文字描述,希望对大家有所帮助。

本篇文章我们来了解 Vue2.X 响应式原理,然后我们来实现一个 vue 响应式原理(写的内容简单)实现步骤和注释写的很清晰,大家有兴趣可以耐心观看,希望对大家有所帮助!


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools
