Skip to content

Commit 17ab220

Browse files
authored
fix: deduplicate change detection caveat sections (#2497)
* fix: deduplicate change detection caveat sections * fix: apply suggestions from review
1 parent ad6b6e2 commit 17ab220

File tree

2 files changed

+47
-99
lines changed

2 files changed

+47
-99
lines changed

src/v2/guide/list.md

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -260,103 +260,7 @@ You might think this will cause Vue to throw away the existing DOM and re-render
260260

261261
### Caveats
262262

263-
Due to limitations in JavaScript, Vue **cannot** detect the following changes to an array:
264-
265-
1. When you directly set an item with the index, e.g. `vm.items[indexOfItem] = newValue`
266-
2. When you modify the length of the array, e.g. `vm.items.length = newLength`
267-
268-
For example:
269-
270-
``` js
271-
var vm = new Vue({
272-
data: {
273-
items: ['a', 'b', 'c']
274-
}
275-
})
276-
vm.items[1] = 'x' // is NOT reactive
277-
vm.items.length = 2 // is NOT reactive
278-
```
279-
280-
To overcome caveat 1, both of the following will accomplish the same as `vm.items[indexOfItem] = newValue`, but will also trigger state updates in the reactivity system:
281-
282-
``` js
283-
// Vue.set
284-
Vue.set(vm.items, indexOfItem, newValue)
285-
```
286-
``` js
287-
// Array.prototype.splice
288-
vm.items.splice(indexOfItem, 1, newValue)
289-
```
290-
291-
You can also use the [`vm.$set`](https://vuejs.org/v2/api/#vm-set) instance method, which is an alias for the global `Vue.set`:
292-
293-
``` js
294-
vm.$set(vm.items, indexOfItem, newValue)
295-
```
296-
297-
To deal with caveat 2, you can use `splice`:
298-
299-
``` js
300-
vm.items.splice(newLength)
301-
```
302-
303-
## Object Change Detection Caveats
304-
305-
Again due to limitations of modern JavaScript, **Vue cannot detect property addition or deletion**. For example:
306-
307-
``` js
308-
var vm = new Vue({
309-
data: {
310-
a: 1
311-
}
312-
})
313-
// `vm.a` is now reactive
314-
315-
vm.b = 2
316-
// `vm.b` is NOT reactive
317-
```
318-
319-
Vue does not allow dynamically adding new root-level reactive properties to an already created instance. However, it's possible to add reactive properties to a nested object using the `Vue.set(object, propertyName, value)` method. For example, given:
320-
321-
``` js
322-
var vm = new Vue({
323-
data: {
324-
userProfile: {
325-
name: 'Anika'
326-
}
327-
}
328-
})
329-
```
330-
331-
You could add a new `age` property to the nested `userProfile` object with:
332-
333-
``` js
334-
Vue.set(vm.userProfile, 'age', 27)
335-
```
336-
337-
You can also use the `vm.$set` instance method, which is an alias for the global `Vue.set`:
338-
339-
``` js
340-
vm.$set(vm.userProfile, 'age', 27)
341-
```
342-
343-
Sometimes you may want to assign a number of new properties to an existing object, for example using `Object.assign()` or `_.extend()`. In such cases, you should create a fresh object with properties from both objects. So instead of:
344-
345-
``` js
346-
Object.assign(vm.userProfile, {
347-
age: 27,
348-
favoriteColor: 'Vue Green'
349-
})
350-
```
351-
352-
You would add new, reactive properties with:
353-
354-
``` js
355-
vm.userProfile = Object.assign({}, vm.userProfile, {
356-
age: 27,
357-
favoriteColor: 'Vue Green'
358-
})
359-
```
263+
Due to limitations in JavaScript, there are types of changes that Vue **cannot detect** with arrays and objects. These are discussed in the [reactivity](reactivity.html#Change-Detection-Caveats) section.
360264

361265
## Displaying Filtered/Sorted Results
362266

src/v2/guide/reactivity.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ Every component instance has a corresponding **watcher** instance, which records
2020

2121
## Change Detection Caveats
2222

23-
Due to the limitations of modern JavaScript (and the abandonment of `Object.observe`), Vue **cannot detect property addition or deletion**. Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the `data` object in order for Vue to convert it and make it reactive. For example:
23+
Due to limitations in JavaScript, there are types of changes that Vue **cannot detect**. However, there are ways to circumvent them to preserve reactivity.
24+
25+
### For Objects
26+
27+
Vue cannot detect property addition or deletion. Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the `data` object in order for Vue to convert it and make it reactive. For example:
2428

2529
``` js
2630
var vm = new Vue({
@@ -53,7 +57,47 @@ Sometimes you may want to assign a number of properties to an existing object, f
5357
this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })
5458
```
5559

56-
There are also a few array-related caveats, which were discussed earlier in the [list rendering section](list.html#Caveats).
60+
### For Arrays
61+
62+
Vue cannot detect the following changes to an array:
63+
64+
1. When you directly set an item with the index, e.g. `vm.items[indexOfItem] = newValue`
65+
2. When you modify the length of the array, e.g. `vm.items.length = newLength`
66+
67+
For example:
68+
69+
``` js
70+
var vm = new Vue({
71+
data: {
72+
items: ['a', 'b', 'c']
73+
}
74+
})
75+
vm.items[1] = 'x' // is NOT reactive
76+
vm.items.length = 2 // is NOT reactive
77+
```
78+
79+
To overcome caveat 1, both of the following will accomplish the same as `vm.items[indexOfItem] = newValue`, but will also trigger state updates in the reactivity system:
80+
81+
``` js
82+
// Vue.set
83+
Vue.set(vm.items, indexOfItem, newValue)
84+
```
85+
``` js
86+
// Array.prototype.splice
87+
vm.items.splice(indexOfItem, 1, newValue)
88+
```
89+
90+
You can also use the [`vm.$set`](https://vuejs.org/v2/api/#vm-set) instance method, which is an alias for the global `Vue.set`:
91+
92+
``` js
93+
vm.$set(vm.items, indexOfItem, newValue)
94+
```
95+
96+
To deal with caveat 2, you can use `splice`:
97+
98+
``` js
99+
vm.items.splice(newLength)
100+
```
57101

58102
## Declaring Reactive Properties
59103

0 commit comments

Comments
 (0)