How to use shallowRef and shallowReactive in Vue3
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

tinymce is a fully functional rich text editor plug-in, but introducing tinymce into vue is not as smooth as other Vue rich text plug-ins. tinymce itself is not suitable for Vue, and @tinymce/tinymce-vue needs to be introduced, and It is a foreign rich text plug-in and has not passed the Chinese version. You need to download the translation package from its official website (you may need to bypass the firewall). 1. Install related dependencies npminstalltinymce-Snpminstall@tinymce/tinymce-vue-S2. Download the Chinese package 3. Introduce the skin and Chinese package. Create a new tinymce folder in the project public folder and download the

To achieve partial refresh of the page, we only need to implement the re-rendering of the local component (dom). In Vue, the easiest way to achieve this effect is to use the v-if directive. In Vue2, in addition to using the v-if instruction to re-render the local dom, we can also create a new blank component. When we need to refresh the local page, jump to this blank component page, and then jump back in the beforeRouteEnter guard in the blank component. original page. As shown in the figure below, how to click the refresh button in Vue3.X to reload the DOM within the red box and display the corresponding loading status. Since the guard in the component in the scriptsetup syntax in Vue3.X only has o

Vue implements the blog front-end and needs to implement markdown parsing. If there is code, it needs to implement code highlighting. There are many markdown parsing libraries for Vue, such as markdown-it, vue-markdown-loader, marked, vue-markdown, etc. These libraries are all very similar. Marked is used here, and highlight.js is used as the code highlighting library. The specific implementation steps are as follows: 1. Install dependent libraries. Open the command window under the vue project and enter the following command npminstallmarked-save//marked to convert markdown into htmlnpmins

After the vue3 project is packaged and published to the server, the access page displays blank 1. The publicPath in the vue.config.js file is processed as follows: const{defineConfig}=require('@vue/cli-service') module.exports=defineConfig({publicPath :process.env.NODE_ENV==='production'?'./':'/&

The final effect is to install the VueCropper component yarnaddvue-cropper@next. The above installation value is for Vue3. If it is Vue2 or you want to use other methods to reference, please visit its official npm address: official tutorial. It is also very simple to reference and use it in a component. You only need to introduce the corresponding component and its style file. I do not reference it globally here, but only introduce import{userInfoByRequest}from'../js/api' in my component file. import{VueCropper}from'vue-cropper&

Preface Whether it is vue or react, when we encounter multiple repeated codes, we will think about how to reuse these codes instead of filling a file with a bunch of redundant codes. In fact, both vue and react can achieve reuse by extracting components, but if you encounter some small code fragments and you don’t want to extract another file, in comparison, react can be used in the same Declare the corresponding widget in the file, or implement it through renderfunction, such as: constDemo:FC=({msg})=>{returndemomsgis{msg}}constApp:FC=()=>{return(

vue3+ts+axios+pinia realizes senseless refresh 1. First download aiXos and pinianpmipinia in the project--savenpminstallaxios--save2. Encapsulate axios request-----Download js-cookienpmiJS-cookie-s//Introduce aixosimporttype{AxiosRequestConfig ,AxiosResponse}from"axios";importaxiosfrom'axios';import{ElMess
