Skip to content

Commit a4b6494

Browse files
Addressed reviewers comments
1 parent 8bdb2c3 commit a4b6494

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

src/api/composition-api.md

+36-36
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The `setup` function is a new component option. It serves as the entry point for
88

99
- **Invocation Timing**
1010

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.
1212

1313
- **Arguments**
1414

@@ -25,7 +25,7 @@ The `setup` function is a new component option. It serves as the entry point for
2525
}
2626
```
2727

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`:
2929

3030
```js
3131
export default {
@@ -40,7 +40,7 @@ The `setup` function is a new component option. It serves as the entry point for
4040
}
4141
```
4242

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:
4444

4545
```js
4646
export default {
@@ -55,9 +55,9 @@ The `setup` function is a new component option. It serves as the entry point for
5555
}
5656
```
5757

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.
5959

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:
6161

6262
```js
6363
const MyComponent = {
@@ -125,7 +125,7 @@ Note that refs returned from `setup` are automatically unwrapped when accessed i
125125

126126
- **Usage with Render Functions**
127127

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:
129129

130130
```js
131131
import { h, ref, reactive } from 'vue'
@@ -183,7 +183,7 @@ Takes an object and returns a reactive proxy of the original.
183183
const obj = reactive({ count: 0 })
184184
```
185185

186-
The reactive conversion is "deep": it affects all nested properties. In the [ES2015 Proxy](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+
The reactive conversion is "deep"it affects all nested properties. In the [ES2015 Proxy](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.
187187

188188
- **Typing**
189189

@@ -273,7 +273,7 @@ If an object is assigned as a ref's value, the object is made deeply reactive by
273273
console.log(count.value) // 1
274274
```
275275

276-
Note that if a new ref is assigned to a property linked to an existing ref, it will replace the old ref:
276+
If a new ref is assigned to a property linked to an existing ref, it will replace the old ref:
277277

278278
```js
279279
const otherCount = ref(2)
@@ -283,7 +283,7 @@ If an object is assigned as a ref's value, the object is made deeply reactive by
283283
console.log(count.value) // 1
284284
```
285285

286-
Note that ref unwrapping only happens when nested inside a reactive `Object`. There is no unwrapping performed when the ref is accessed from an `Array` or a native collection type like `Map`:
286+
Ref unwrapping only happens when nested inside a reactive `Object`. There is no unwrapping performed when the ref is accessed from an `Array` or a native collection type like `Map`:
287287

288288
```js
289289
const arr = reactive([ref(0)])
@@ -377,7 +377,7 @@ copy.count++ // warning!
377377

378378
### `watchEffect`
379379

380-
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.
381381

382382
```js
383383
const count = ref(0)
@@ -393,7 +393,7 @@ setTimeout(() => {
393393

394394
#### Stopping the Watcher
395395

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.
397397

398398
In other cases, it returns a stop handle which can be called to explicitly stop the watcher:
399399

@@ -408,7 +408,7 @@ stop()
408408

409409
#### Side Effect Invalidation
410410

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:
412412

413413
- the effect is about to re-run
414414
- 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
437437

438438
#### Effect Flush Timing
439439

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:
441441

442442
```html
443443
<template>
@@ -476,7 +476,7 @@ onMounted(() => {
476476
})
477477
```
478478

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'`):
480480

481481
```js
482482
// fire synchronously
@@ -522,7 +522,7 @@ watchEffect(
522522
)
523523
```
524524

525-
`onTrack` and `onTrigger` only works in development mode.
525+
`onTrack` and `onTrigger` only work in development mode.
526526

527527
- **Typing**
528528

@@ -552,7 +552,7 @@ watchEffect(
552552
553553
### `watch`
554554
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.
556556
557557
- Compared to `watchEffect`, `watch` allows us to:
558558
@@ -583,7 +583,7 @@ The `watch` API is the exact equivalent of the Options API [`this.$watch`](./ins
583583

584584
- **Watching Multiple Sources**
585585

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:
587587

588588
```js
589589
watch([fooRef, barRef], ([foo, bar], [prevFoo, prevBar]) => {
@@ -635,7 +635,7 @@ The `watch` API is the exact equivalent of the Options API [`this.$watch`](./ins
635635

636636
## Lifecycle Hooks
637637

638-
Lifecycle hooks can be registered with directly imported `onXXX` functions:
638+
Lifecycle hooks can be registered with directly-imported `onX` functions:
639639

640640
```js
641641
import { onMounted, onUpdated, onUnmounted } from 'vue'
@@ -702,7 +702,7 @@ const Descendent = {
702702

703703
- **Injection Reactivity**
704704

705-
To retain reactivity between provided and injected values, a ref can be used:
705+
To retain reactivity between provided and injected values, we can use a ref:
706706

707707
```js
708708
// in provider
@@ -731,7 +731,7 @@ const Descendent = {
731731
function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T
732732
```
733733

734-
Vue provides a `InjectionKey` interface which is a generic type that extends `Symbol`. It can be used to sync the type of the injected value between the provider and the consumer:
734+
Vue provides an `InjectionKey` interface which is a generic type that extends `Symbol`. It can be used to sync the type of the injected value between the provider and the consumer:
735735

736736
```ts
737737
import { InjectionKey, provide, inject } from 'vue'
@@ -778,7 +778,7 @@ When using the Composition API, the concept of _reactive refs_ and [template ref
778778
</script>
779779
```
780780

781-
Here we are exposing `root` on the render context and binding it to the div as its ref via `ref="root"`. In the Virtual DOM patching algorithm, if a VNode's `ref` key corresponds to a ref on the render context, then the VNode's corresponding element or component instance will be assigned to the value of that ref. This is performed during the Virtual DOM mount / patch process, so template refs will only get assigned values after the initial render.
781+
Here we are exposing `root` on the render context and binding it to the div as its ref via `ref="root"`. In the Virtual DOM patching algorithm, if a VNode's `ref` key corresponds to a ref on the render context, the VNode's corresponding element or component instance will be assigned to the value of that ref. This is performed during the Virtual DOM mount / patch process, so template refs will only get assigned values after the initial render.
782782

783783
Refs used as templates refs behave just like any other refs: they are reactive and can be passed into (or returned from) composition functions.
784784

@@ -851,7 +851,7 @@ function useFoo(x: number | Ref<number>) {
851851

852852
### `toRef`
853853

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.
855855

856856
```js
857857
const state = reactive({
@@ -880,7 +880,7 @@ export default {
880880

881881
### `toRefs`
882882

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.
884884

885885
```js
886886
const state = reactive({
@@ -906,7 +906,7 @@ stateAsRefs.foo.value++
906906
console.log(state.foo) // 3
907907
```
908908

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:
910910

911911
```js
912912
function useFeatureX() {
@@ -936,15 +936,15 @@ export default {
936936

937937
### `isRef`
938938

939-
Check if a value is a ref object.
939+
Checks if a value is a ref object.
940940

941941
### `isProxy`
942942

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`.
944944

945945
### `isReactive`
946946

947-
Check if an object is a reactive proxy created by `reactive`.
947+
Checks if an object is a reactive proxy created by `reactive`.
948948

949949
```js
950950
import { reactive, isReactive } from 'vue'
@@ -982,13 +982,13 @@ export default {
982982

983983
### `isReadonly`
984984

985-
Check if an object is a readonly proxy created by `readonly`.
985+
Checks if an object is a readonly proxy created by `readonly`.
986986

987987
## Advanced Reactivity APIs
988988

989989
### `customRef`
990990

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`.
992992

993993
- Example using a custom ref to implement debounce with `v-model`:
994994

@@ -1041,7 +1041,7 @@ Create a customized ref with explicit control over its dependency tracking and u
10411041
10421042
### `markRaw`
10431043
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.
10451045
10461046
```js
10471047
const foo = markRaw({})
@@ -1053,7 +1053,7 @@ console.log(isReactive(bar.foo)) // false
10531053
```
10541054

10551055
::: warning
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:
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:
10571057

10581058
- Some values simply should not be made reactive, for example a complex 3rd party class instance, or a Vue component object.
10591059

@@ -1074,12 +1074,12 @@ const bar = reactive({
10741074
console.log(foo.nested === bar.nested) // false
10751075
```
10761076

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.
10781078
:::
10791079

10801080
### `shallowReactive`
10811081

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).
10831083

10841084
```js
10851085
const state = shallowReactive({
@@ -1098,7 +1098,7 @@ state.nested.bar++ // non-reactive
10981098

10991099
### `shallowReadonly`
11001100

1101-
Create a proxy that makes its own properties readonly, but does not perform deep readonly conversion of nested objects (exposes raw values).
1101+
Creates a proxy that makes its own properties readonly, but does not perform deep readonly conversion of nested objects (exposes raw values).
11021102

11031103
```js
11041104
const state = shallowReadonly({
@@ -1117,7 +1117,7 @@ state.nested.bar++ // works
11171117

11181118
### `shallowRef`
11191119

1120-
Create a ref that tracks its own `.value` mutation but doesn't make its value reactive.
1120+
Creates a ref that tracks its own `.value` mutation but doesn't make its value reactive.
11211121

11221122
```js
11231123
const foo = shallowRef({})
@@ -1129,7 +1129,7 @@ isReactive(foo.value) // false
11291129

11301130
### `toRaw`
11311131

1132-
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.
11331133

11341134
```js
11351135
const foo = {}

0 commit comments

Comments
 (0)