You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/api/composition-api.md
+36-36
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ The `setup` function is a new component option. It serves as the entry point for
8
8
9
9
-**Invocation Timing**
10
10
11
-
`setup` is called right after the initial props resolution when a component instance is created. Lifecycle-wise, it is called before the `beforeCreate` hook.
11
+
`setup` is called right after the initial props resolution when a component instance is created. [Lifecycle-wise](/guide/instance.html#instance-lifecycle-hooks), it is called before the `beforeCreate` hook.
12
12
13
13
-**Arguments**
14
14
@@ -25,7 +25,7 @@ The `setup` function is a new component option. It serves as the entry point for
25
25
}
26
26
```
27
27
28
-
Note this `props` object is reactive - i.e. it is updated when new props are passed in, and can be observed and reacted upon using `watchEffect` or `watch`:
28
+
Note that this `props` object is reactive - i.e. it is updated when new props are passed in, and can be observed and reacted upon using `watchEffect` or `watch`:
29
29
30
30
```js
31
31
exportdefault {
@@ -40,7 +40,7 @@ The `setup` function is a new component option. It serves as the entry point for
40
40
}
41
41
```
42
42
43
-
However, **do NOT destructure** the `props` object, as it will lose reactivity:
43
+
However, **do NOT destructure** the `props` object! If you do so, the unpacked values won't have reactivity:
44
44
45
45
```js
46
46
exportdefault {
@@ -55,9 +55,9 @@ The `setup` function is a new component option. It serves as the entry point for
55
55
}
56
56
```
57
57
58
-
The `props` object is immutable during development (will emit warning if user code attempts to mutate it).
58
+
The `props` object is immutable during development. Vue will emit a warning if there's an attempt to mutate it.
59
59
60
-
The second argument provides a context object which exposes a selective list of properties that were previously exposed on `this` in 2.x APIs:
60
+
The second argument of `setup`provides a context object which exposes a selective list of the properties that were previously exposed on `this` in 2.x APIs:
61
61
62
62
```js
63
63
constMyComponent= {
@@ -125,7 +125,7 @@ Note that refs returned from `setup` are automatically unwrapped when accessed i
125
125
126
126
-**Usage with Render Functions**
127
127
128
-
`setup` can also return a render function, which can directly make use of the reactive state declared in the same scope:
128
+
`setup` can also return a render function which can directly make use of the reactive state declared in the same scope:
129
129
130
130
```js
131
131
import { h, ref, reactive } from'vue'
@@ -183,7 +183,7 @@ Takes an object and returns a reactive proxy of the original.
183
183
const obj = reactive({ count: 0 })
184
184
```
185
185
186
-
Thereactiveconversionis"deep": itaffectsallnestedproperties. Inthe [ES2015Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) based implementation, the returned proxy is **not** equal to the original object. It is recommended to work exclusively with the reactive proxy and avoid relying on the original object.
186
+
Thereactiveconversionis"deep"—itaffectsallnestedproperties. Inthe [ES2015Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) based implementation, the returned proxy is **not** equal to the original object. It is recommended to work exclusively with the reactive proxy and avoid relying on the original object.
187
187
188
188
-**Typing**
189
189
@@ -273,7 +273,7 @@ If an object is assigned as a ref's value, the object is made deeply reactive by
Run a function immediately while reactively tracking its dependencies, and re-run it whenever the dependencies have changed.
380
+
Runs a function immediately while reactively tracking its dependencies and re-runs it whenever the dependencies are changed.
381
381
382
382
```js
383
383
constcount=ref(0)
@@ -393,7 +393,7 @@ setTimeout(() => {
393
393
394
394
#### Stopping the Watcher
395
395
396
-
When `watchEffect` is called during a component's `setup()` function or lifecycle hooks, the watcher is linked to the component's lifecycle, and will be automatically stopped when the component is unmounted.
396
+
When `watchEffect` is called during a component's `setup()` function or lifecycle hooks, the watcher is linked to the component's lifecycle and will be automatically stopped when the component is unmounted.
397
397
398
398
In other cases, it returns a stop handle which can be called to explicitly stop the watcher:
399
399
@@ -408,7 +408,7 @@ stop()
408
408
409
409
#### Side Effect Invalidation
410
410
411
-
Sometimes the watched effect function will perform async side effects that need to be cleaned up when it is invalidated (i.e state changed before the effects can be completed). The effect function receives an `onInvalidate` function that can be used to register a invalidation callback. The invalidation callback is called when:
411
+
Sometimes the watched effect function will perform asynchronous side effects that need to be cleaned up when it is invalidated (i.e state changed before the effects can be completed). The effect function receives an `onInvalidate` function that can be used to register an invalidation callback. This invalidation callback is called when:
412
412
413
413
- the effect is about to re-run
414
414
- the watcher is stopped (i.e. when the component is unmounted if `watchEffect` is used inside `setup()` or lifecycle hooks)
@@ -437,7 +437,7 @@ An async function implicitly returns a Promise, but the cleanup function needs t
437
437
438
438
#### Effect Flush Timing
439
439
440
-
Vue's reactivity system buffers invalidated effects and flush them asynchronously to avoid unnecessary duplicate invocation when there are many state mutations happening in the same "tick". Internally, a component's update function is also a watched effect. When a user effect is queued, it is always invoked after all component update effects:
440
+
Vue's reactivity system buffers invalidated effects and flushes them asynchronously to avoid unnecessary duplicate invocation when there are many state mutations happening in the same "tick". Internally, a component's `update` function is also a watched effect. When a user effect is queued, it is always invoked after all component `update` effects:
441
441
442
442
```html
443
443
<template>
@@ -476,7 +476,7 @@ onMounted(() => {
476
476
})
477
477
```
478
478
479
-
In cases where a watcher effect needs to be re-run synchronously or before component updates, we can pass an additional options object with the `flush` option (default is `'post'`):
479
+
In cases where a watcher effect needs to be re-run synchronously or before component updates, we can pass an additional `options` object with the `flush` option (default is `'post'`):
480
480
481
481
```js
482
482
// fire synchronously
@@ -522,7 +522,7 @@ watchEffect(
522
522
)
523
523
```
524
524
525
-
`onTrack` and `onTrigger` only works in development mode.
525
+
`onTrack` and `onTrigger` only work in development mode.
526
526
527
527
-**Typing**
528
528
@@ -552,7 +552,7 @@ watchEffect(
552
552
553
553
### `watch`
554
554
555
-
The `watch` API is the exact equivalent of the Options API [`this.$watch`](./instance-methods.html#watch) (and the corresponding `watch` option). `watch` requires watching a specific data source, and applies side effects in a separate callback function. It also is lazy by default - i.e. the callback is only called when the watched source has changed.
555
+
The `watch` API is the exact equivalent of the Options API [`this.$watch`](./instance-methods.html#watch) (and the corresponding `watch` option). `watch` requires watching a specific data source and applies side effects in a separate callback function. It also is lazy by default - i.e. the callback is only called when the watched source has changed.
556
556
557
557
- Compared to `watchEffect`, `watch` allows us to:
558
558
@@ -583,7 +583,7 @@ The `watch` API is the exact equivalent of the Options API [`this.$watch`](./ins
583
583
584
584
-**Watching Multiple Sources**
585
585
586
-
A watcher can also watch multiple sources at the same time using an Array:
586
+
A watcher can also watch multiple sources at the same time using an array:
import { InjectionKey, provide, inject } from 'vue'
@@ -778,7 +778,7 @@ When using the Composition API, the concept of _reactive refs_ and [template ref
778
778
</script>
779
779
```
780
780
781
-
Hereweareexposing`root`ontherendercontextandbindingittothedivasitsrefvia`ref="root"`. IntheVirtualDOMpatchingalgorithm, ifaVNode's `ref` key corresponds to a ref on the render context, then the VNode'scorrespondingelementorcomponentinstancewillbeassignedtothevalueofthatref. ThisisperformedduringtheVirtualDOMmount/patchprocess, sotemplaterefswillonlygetassignedvaluesaftertheinitialrender.
781
+
Hereweareexposing`root`ontherendercontextandbindingittothedivasitsrefvia`ref="root"`. IntheVirtualDOMpatchingalgorithm, ifaVNode's `ref` key corresponds to a ref on the render context, the VNode'scorrespondingelementorcomponentinstancewillbeassignedtothevalueofthatref. ThisisperformedduringtheVirtualDOMmount/patchprocess, sotemplaterefswillonlygetassignedvaluesaftertheinitialrender.
@@ -851,7 +851,7 @@ function useFoo(x: number | Ref<number>) {
851
851
852
852
### `toRef`
853
853
854
-
`toRef` can be used to create a ref for a property on a source reactive object. The ref can then be passed around and retains the reactive connection to its source property.
854
+
Can be used to create a ref for a property on a source reactive object. The ref can then be passed around, retaining the reactive connection to its source property.
855
855
856
856
```js
857
857
conststate=reactive({
@@ -880,7 +880,7 @@ export default {
880
880
881
881
### `toRefs`
882
882
883
-
Convert a reactive object to a plain object, where each property on the resulting object is a ref pointing to the corresponding property in the original object.
883
+
Converts a reactive object to a plain object where each property of the resulting object is a ref pointing to the corresponding property of the original object.
884
884
885
885
```js
886
886
conststate=reactive({
@@ -906,7 +906,7 @@ stateAsRefs.foo.value++
906
906
console.log(state.foo) // 3
907
907
```
908
908
909
-
`toRefs` is useful when returning a reactive object from a composition function so that the consuming component can destructure / spread the returned object without losing reactivity:
909
+
`toRefs` is useful when returning a reactive object from a composition function so that the consuming component can destructure/spread the returned object without losing reactivity:
910
910
911
911
```js
912
912
functionuseFeatureX() {
@@ -936,15 +936,15 @@ export default {
936
936
937
937
### `isRef`
938
938
939
-
Check if a value is a ref object.
939
+
Checks if a value is a ref object.
940
940
941
941
### `isProxy`
942
942
943
-
Check if an object is a proxy created by `reactive` or `readonly`.
943
+
Checks if an object is a proxy created by `reactive` or `readonly`.
944
944
945
945
### `isReactive`
946
946
947
-
Check if an object is a reactive proxy created by `reactive`.
947
+
Checks if an object is a reactive proxy created by `reactive`.
948
948
949
949
```js
950
950
import { reactive, isReactive } from'vue'
@@ -982,13 +982,13 @@ export default {
982
982
983
983
### `isReadonly`
984
984
985
-
Check if an object is a readonly proxy created by `readonly`.
985
+
Checks if an object is a readonly proxy created by `readonly`.
986
986
987
987
## Advanced Reactivity APIs
988
988
989
989
### `customRef`
990
990
991
-
Create a customized ref with explicit control over its dependency tracking and update triggering. It expects a factory function. The factory function receives `track` and `trigger` functions as arguments and should return an object with `get` and `set`.
991
+
Creates a customized ref with explicit control over its dependency tracking and updates triggering. It expects a factory function, which receives `track` and `trigger` functions as arguments and should return an object with `get` and `set`.
992
992
993
993
- Example using a custom ref to implement debounce with `v-model`:
994
994
@@ -1041,7 +1041,7 @@ Create a customized ref with explicit control over its dependency tracking and u
1041
1041
1042
1042
### `markRaw`
1043
1043
1044
-
Mark an object so that it will never be converted to a proxy. Returns the object itself.
1044
+
Marks an object so that it will never be converted to a proxy. Returns the object itself.
`markRaw` and the shallowXXX APIs below allow you to selectively opt-out of the default deep reactive / readonly conversion and embed raw, non-proxied objects in your state graph. They can be used for various reasons:
1056
+
`markRaw` and the shallowXXX APIs below allow you to selectively opt-out of the default deep reactive/readonly conversion and embed raw, non-proxied objects in your state graph. They can be used for various reasons:
1057
1057
1058
1058
- Some values simply should not be made reactive, for example a complex 3rd party class instance, or a Vue component object.
1059
1059
@@ -1074,12 +1074,12 @@ const bar = reactive({
1074
1074
console.log(foo.nested===bar.nested) // false
1075
1075
```
1076
1076
1077
-
Identity hazards are in general rare. But to properly utilize these APIs while safely avoiding identity hazards requires a solid understanding of how the reactivity system works.
1077
+
Identity hazards are in general rare. However, to properly utilize these APIs while safely avoiding identity hazards requires a solid understanding of how the reactivity system works.
1078
1078
:::
1079
1079
1080
1080
### `shallowReactive`
1081
1081
1082
-
Create a reactive proxy that tracks reactivity of its own properties, but does not perform deep reactive conversion of nested objects.
1082
+
Creates a reactive proxy that tracks reactivity of its own properties but does not perform deep reactive conversion of nested objects (exposes raw values).
Return the raw, original object of a `reactive` or `readonly` proxy. This is an escape hatch that can be used to temporarily read without incurring proxy access / tracking overhead or write without triggering changes. It is **not** recommended to hold a persistent reference to the original object. Use with caution.
1132
+
Returns the raw, original object of a `reactive` or `readonly` proxy. This is an escape hatch that can be used to temporarily read without incurring proxy access/tracking overhead or write without triggering changes. It is **not** recommended to hold a persistent reference to the original object. Use with caution.
0 commit comments