How to use the shallowReactive function from vue

Find comprehensive JavaScript vue.shallowReactive code examples handpicked from public code repositorys.

vue.shallowReactive is a function in the Vue.js framework that creates a shallowly-reactive object with its own reactive properties, but not its nested properties.

176
177
178
179
180
181
182
183
184
185
186
187
188
189


  return Router;
}();


function createRouter(options) {
  var router = vue.shallowReactive(new Router(options));


  router._init();


  router.install = function (app) {
fork icon0
star icon0
watch icon1

+ 31 other calls in file

How does vue.shallowReactive work?

The vue.shallowReactive function is a part of the Vue.js framework, which is a reactive and component-based JavaScript library for building user interfaces. When you call the vue.shallowReactive function, you pass in an object as the first argument. The function then creates a shallowly-reactive version of the object, where the top-level properties of the object are reactive, but the nested properties are not. This means that if you modify a nested property of the object, the reactivity system will not detect the change and will not trigger any updates to the component that is using the object. However, if you modify a top-level property of the object, the reactivity system will detect the change and will trigger updates to the component that is using the object. Overall, the vue.shallowReactive function provides a way to create a reactive object that is optimized for performance, because it only tracks changes to top-level properties and not nested properties. It's worth noting that the vue.shallowReactive function is designed to be used with Vue.js components, and may not be appropriate for all use cases. If you need full reactivity on an object, including nested properties, you should consider using the vue.reactive function instead.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { shallowReactive } from "vue";

export default {
  setup() {
    const user = shallowReactive({
      name: "John",
      age: 30,
      address: {
        city: "New York",
        state: "NY",
      },
    });

    user.name = "Jane"; // triggers an update
    user.address.city = "San Francisco"; // does not trigger an update

    return { user };
  },
};

In this example, we first import the shallowReactive function from the vue module in our Vue.js component. We then call the shallowReactive function to create a shallowly-reactive object named user. The user object has three properties: name, age, and address. The address property is an object with its own properties, city and state. We then modify the name property of the user object, which triggers an update to the component. We also modify the city property of the address property of the user object, but this does not trigger an update to the component, because the address property is not reactive. Finally, we return the user object from the setup function, so that it can be used in the template of the component. Overall, this example demonstrates how to use vue.shallowReactive to create a shallowly-reactive object in a Vue.js component, where the top-level properties of the object are reactive, but the nested properties are not.