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: active-rfcs/0000-advanced-reactivity-api.md
+25-25Lines changed: 25 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -21,13 +21,13 @@ watch(() => obj.a, value => {
21
21
console.log(`obj.a is: ${value}`)
22
22
})
23
23
24
-
// a "ref" object that has a .value property
24
+
// a "pointer" object that has a .value property
25
25
constcount=value(0)
26
26
27
-
// computed "ref" with a read-only .value property
27
+
// computed "pointer" with a read-only .value property
28
28
constplusOne=computed(() =>count.value+1)
29
29
30
-
//refs can be watched directly
30
+
//pointers can be watched directly
31
31
watch(count, (count, oldCount) => {
32
32
console.log(`count is: ${count}`)
33
33
})
@@ -71,7 +71,7 @@ const object = state({
71
71
72
72
This works exactly like 2.6 `Vue.observable`. The returned object behaves just like a normal object, and when its properties are accessed in **reactive computations** (render functions, computed property getters and watcher getters), they are tracked as dependencies. Mutation to these properties will cause corresponding computations to re-run.
73
73
74
-
## Value Refs
74
+
## Value Pointers
75
75
76
76
The `state` API cannot be used for primitive values because:
77
77
@@ -84,24 +84,24 @@ The simple solution is wrapping the value in an object wrapper that can be passe
84
84
```js
85
85
import { value } from'vue'
86
86
87
-
constcountRef=value(0)
87
+
constcountPointer=value(0)
88
88
```
89
89
90
-
The `value` API creates a wrapper object for a value, called a **ref**. A ref is a reactive object with a single property: `.value`. The property points to the actual value being held and is writable:
90
+
The `value` API creates a wrapper object for a value, called a **pointer** (This is not technically a C pointer, but the concept is quite close). A pointer is a reactive object with a single property: `.value`. The property points to the actual value being held and is writable:
91
91
92
92
```js
93
93
// read the value
94
-
console.log(countRef.value) // 0
94
+
console.log(countPointer.value) // 0
95
95
96
96
// mutate the value
97
-
countRef.value++
97
+
countPointer.value++
98
98
```
99
99
100
-
Refs are primarily used for holding primitive values, but it can also hold any other values including deeply nested objects and arrays. Non-primitive values held inside a ref behave like normal reactive objects created via `state`.
100
+
Pointers are primarily used for holding primitive values, but it can also hold any other values including deeply nested objects and arrays. Non-primitive values held inside a pointer behave like normal reactive objects created via `state`.
101
101
102
-
## Computed Refs
102
+
## Computed Pointers
103
103
104
-
In addition to plain value refs, we can also create computed refs:
104
+
In addition to plain value pointers, we can also create computed pointers:
105
105
106
106
```js
107
107
import { value, computed } from'vue'
@@ -114,12 +114,12 @@ count.value++
114
114
console.log(countPlusOne.value) // 2
115
115
```
116
116
117
-
Computed refs are readonly by default - assigning to its `value` property will result in an error.
117
+
Computed pointers are readonly by default - assigning to its `value` property will result in an error.
118
118
119
-
Computed refs can be made writable by passing a write callback as the 2nd argument:
119
+
Computed pointers can be made writable by passing a write callback as the 2nd argument:
120
120
121
121
```js
122
-
constwritableRef=computed(
122
+
constwritablePointer=computed(
123
123
// read
124
124
() =>count.value+1,
125
125
// write
@@ -129,7 +129,7 @@ const writableRef = computed(
129
129
)
130
130
```
131
131
132
-
Computed refs behaves like computed properties in a component: it tracks its dependencies and only re-evaluates when dependencies have changed.
132
+
Computed pointers behave like computed properties in a component: it tracks its dependencies and only re-evaluates when dependencies have changed.
133
133
134
134
## Watchers
135
135
@@ -174,15 +174,15 @@ count.value++
174
174
// -> count + 1 is: 2
175
175
```
176
176
177
-
### Watching Refs
177
+
### Watching Pointers
178
178
179
-
The 1st argument can also be a ref:
179
+
The 1st argument can also be a pointer:
180
180
181
181
```js
182
-
// double is a computed ref
182
+
// double is a computed pointer
183
183
constdouble=computed(() =>count.value*2)
184
184
185
-
// watch a ref directly
185
+
// watch a pointer directly
186
186
watch(double, value=> {
187
187
console.log('double the count is: ', value)
188
188
})
@@ -224,7 +224,7 @@ The **effect** callback can also return a cleanup function which gets called eve
224
224
- the watcher is stopped
225
225
226
226
```js
227
-
watch(idRef, id=> {
227
+
watch(idPointer, id=> {
228
228
consttoken=performAsyncOperation(id)
229
229
230
230
return () => {
@@ -249,11 +249,11 @@ watch(
249
249
)
250
250
```
251
251
252
-
## Exposing Refs to Components
252
+
## Exposing Pointers to Components
253
253
254
254
While this proposal is focused on working with reactive state outside of components, such state should also be usable inside components as well.
255
255
256
-
Refs can be returned in a component's `data()` function:
256
+
Pointers can be returned in a component's `data()` function:
257
257
258
258
```js
259
259
import { value } from'vue'
@@ -267,7 +267,7 @@ export default {
267
267
}
268
268
```
269
269
270
-
**When a `ref` is returned as a root-level property in `data()`, it is bound to the component instance as a direct property.** This means there's no need to access the value via `.value` - the value can be accessed and mutated directly as `this.count`, and directly as `count` inside templates:
270
+
**When a pointer is returned as a root-level property in `data()`, it is bound to the component instance as a direct property.** This means there's no need to access the value via `.value` - the value can be accessed and mutated directly as `this.count`, and directly as `count` inside templates:
271
271
272
272
```html
273
273
<div@click="count++">
@@ -281,7 +281,7 @@ The APIs proposed here are just low-level building blocks. Technically, they pro
281
281
282
282
# Drawbacks
283
283
284
-
- To pass state around while keeping them "trackable" and "reactive", values must be passed around in the form of ref containers. This is a new concept and can be a bit more difficult to learn than the base API. However, these APIs are intended for advanced use cases so the learning cost should be acceptable.
284
+
- To pass state around while keeping them "trackable" and "reactive", values must be passed around in wrapper objects (pointers). This is a new concept and can be a bit more difficult to learn than the base API. However, these APIs are intended for advanced use cases so the learning cost should be acceptable.
285
285
286
286
# Alternatives
287
287
@@ -299,4 +299,4 @@ This is mostly new APIs that expose existing internal capabilities. Users famili
299
299
300
300
Sidenote: removing `this.$watch` and the `watch` option also makes the entire `watch` API completely tree-shakable.
301
301
302
-
- We probably need to also expose a `isRef` method to check whether an object is a value/computed ref.
302
+
- We probably need to also expose a `isPointer` method to check whether an object is a value/computed pointer.
0 commit comments