SoFunction
Updated on 2025-04-07

The use of shallowRef for Vue3 responsive higher-order usage

Introduction

In Vue 3, a responsive system is one of its core features. With a responsive system, developers can easily manage and update application status. However, for some special scenarios, we may needMore fine-grained control. At this time,shallowRef()It comes in handy. This article will introduce in detailshallowRef()The usage scenarios, basic usage, detailed functional explanations, best practices and cases help readers better understand and apply this advanced usage.

1. Use scenarios

1.1 Performance optimization of deep nested objects

Vue's default responsive system recursively redirects when dealing with deep nested objectsEach layer of the objectAll are converted to responsive. This can cause performance issues in some cases.shallowRef() It will only make the first layer of the object responsive, thus optimizing performance.

1.2 Scenarios that require partial responsiveness

In some scenarios, we only need some properties of the object to be responsive, while others do not.shallowRef()This requirement can be met, allowing developers to control responsive behavior more flexibly.

2. Basic use

2.1 Introducing shallowRef

In Vue 3,shallowRefCan be passed@vue/reactivityPackage introduction:

import { shallowRef } from 'vue';

2.2 Definition shallowRef

useshallowRefDefine a responsive reference object:

const state = shallowRef({
  user: {
    name: 'Alice',
    age: 25
  },
  isLoggedIn: false
});

In this example,stateThe first layer of the object is responsive, butuserThe properties of the object are not converted into responsive by depth.

3. Detailed explanation of functions

3.1 shallow responsive

shallowRefIt will only make the first layer of the object's attributes responsive:

 = true; // Responsive update = 'Bob'; // Non-responsive update

In the above code,isLoggedInThe changes in attributes will trigger responsive updates, anduserThe object's attribute changes will not be triggered.

3.2 Comparison with ref

andrefcompared to,shallowRefNot recursively converting each layer of an object into a responsive form:

import { ref } from 'vue';

const deepState = ref({
  user: {
    name: 'Alice',
    age: 25
  },
  isLoggedIn: false
});

 = 'Bob'; // Responsive update

In this example,refWill makeuserThe properties of the object also become responsive.

4. Best practices and cases

4.1 Use shallowRef to optimize performance

When processing large amounts of data or deep nested objects, useshallowRefCan significantly improve performance:

const largeData = shallowRef({
  items: ({ length: 10000 }, (_, i) => ({ id: i, value: `Item ${i}` }))
});

4.2 Combining watch and computed

shallowRefCan be withwatchandcomputedUsed in conjunction to provide more flexible responsive control:

import { watch, computed } from 'vue';

const userState = shallowRef({
  user: {
    name: 'Alice',
    age: 25
  }
});

watch(() => , (newName, oldName) => {
  (`User name changed from ${oldName} to ${newName}`);
});

const userName = computed(() => );

5. Summary

shallowRefis a powerful tool in Vue 3 for scenarios where partially responsive or optimized performance is required. Through the introduction of this article, we understandshallowRefDetailed explanation of usage scenarios, basic usage, functions and best practices. I hope these contents can help you better apply them in actual projectsshallowRef, improve development efficiency and application performance.

Through reasonable useshallowRef, we can control responsive behavior more flexibly, thereby building complex Vue 3 applications more efficiently.

This is the end of this article about the use of shallowRef() in Vue3 responsive advanced usage. For more related Vue3 shallowRef() content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!