Home > Article > Web Front-end > How to use shallowRef and shallowReactive in Vue3
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.
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>姓名:{{name}}</h2> <h2>年龄:{{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.
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>姓名:{{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>姓名:{{boy.name}}</h2> <h2>年龄:{{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!