Skip to content

Commit 1d4aec5

Browse files
committed
feedback for watcher and template ref
1 parent 4fbbd64 commit 1d4aec5

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/guide/essentials/template-refs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ While Vue's declarative rendering model abstracts away most of the direct DOM op
66
<input ref="input">
77
```
88

9-
`ref` is a special attribute that is similar to `key` and `is`. It allows us to obtain a direct reference to a specific DOM element or child component instance after it's mounted. This may be useful when you want to, for example, programmatically focus an input on component mount, or initialize a 3rd party library on an element.
9+
`ref` is a special attribute, similar to the `key` attribute discussed in the `v-for` chapter. It allows us to obtain a direct reference to a specific DOM element or child component instance after it's mounted. This may be useful when you want to, for example, programmatically focus an input on component mount, or initialize a 3rd party library on an element.
1010

1111
## Accessing the Refs
1212

src/guide/essentials/watchers.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ export default {
6161
}
6262
```
6363

64-
In addition to the `watch` option, you can also use the imperative [this.$watch API](/api/component-instance.html#watch).
65-
6664
</div>
6765

6866
<div class="composition-api">
@@ -77,7 +75,7 @@ const question = ref('')
7775
const answer = ref('Questions usually contain a question mark. ;-)')
7876
7977
// watch works directly on a ref
80-
watch(question, async (newQuestion) => {
78+
watch(question, async (newQuestion, oldQuestion) => {
8179
if (newQuestion.indexOf('?') > -1) {
8280
answer.value = 'Thinking...'
8381
try {
@@ -183,8 +181,10 @@ When you call `watch()` directly on a reactive object, it will implicitly create
183181
```js
184182
const obj = reactive({ count: 0 })
185183

186-
watch(obj, () => {
184+
watch(obj, (newValue, oldValue) => {
187185
// fires on nested property mutations
186+
// Note: `newValue` will be equal to `oldValue` here
187+
// because they both point to the same object!
188188
})
189189

190190
obj.count++
@@ -194,13 +194,26 @@ This should be differentiated with a getter that returns a reactive object - in
194194

195195
```js
196196
watch(
197-
() => state.activeObject,
197+
() => state.someObject,
198198
() => {
199-
// fires only when state.activeObject is replaced
199+
// fires only when state.someObject is replaced
200200
}
201201
)
202202
```
203203

204+
You can, however, force the second case into a deep watcher by explicitly using the `deep` option:
205+
206+
```js
207+
watch(
208+
() => state.someObject,
209+
(newValue, oldValue) => {
210+
// Note: `newValue` will be equal to `oldValue` here
211+
// *unless* state.someObject has been replaced
212+
},
213+
{ deep: true }
214+
)
215+
```
216+
204217
</div>
205218

206219
:::warning Use with Caution
@@ -265,13 +278,13 @@ You can check out [this example](/examples/#fetching-data) with `watchEffect` an
265278

266279
`watch` and `watchEffect` both allow us to reactively perform side effects. Their main difference is the way they track their reactive dependencies:
267280

268-
- `watch` only tracks the explicitly watched source. It won't track anything accessed inside the callback. This allows us to have more control over what should trigger the callback.
281+
- `watch` only tracks the explicitly watched source. It won't track anything accessed inside the callback. In addition, the callback only triggers when the source has actually changed. `watch` separates dependency tracking from the side effect, giving us more precise control over when the callback should fire.
269282

270-
- `watchEffect`, on the other hand, automatically tracks every reactive property accessed during its synchronous execution. This is more convenient and typically results in terser code, but makes its reactive dependencies less explicit.
283+
- `watchEffect`, on the other hand, combines dependency tracking and side effect into one phase. It automatically tracks every reactive property accessed during its synchronous execution. This is more convenient and typically results in terser code, but makes its reactive dependencies less explicit.
271284

272285
</div>
273286

274-
## Effect Flush Timing
287+
## Callback Flush Timing
275288

276289
Every Vue component has an update function that updates the DOM when relevant state changes. This update function is also a watcher, created by Vue internally.
277290

@@ -323,7 +336,7 @@ watchPostEffect(() => {
323336

324337
<div class="options-api">
325338

326-
## `$watch()` \*
339+
## `this.$watch()` \*
327340

328341
It's also possible to imperatively create watchers using the [`$watch()` instance method](http://localhost:3000/api/component-instance.html#watch):
329342

0 commit comments

Comments
 (0)