Skip to content

Commit b1a9833

Browse files
committed
rename refs to pointers to avoid confusion with DOM refs
1 parent b80def1 commit b1a9833

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

active-rfcs/0000-advanced-reactivity-api.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ watch(() => obj.a, value => {
2121
console.log(`obj.a is: ${value}`)
2222
})
2323

24-
// a "ref" object that has a .value property
24+
// a "pointer" object that has a .value property
2525
const count = value(0)
2626

27-
// computed "ref" with a read-only .value property
27+
// computed "pointer" with a read-only .value property
2828
const plusOne = computed(() => count.value + 1)
2929

30-
// refs can be watched directly
30+
// pointers can be watched directly
3131
watch(count, (count, oldCount) => {
3232
console.log(`count is: ${count}`)
3333
})
@@ -71,7 +71,7 @@ const object = state({
7171

7272
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.
7373

74-
## Value Refs
74+
## Value Pointers
7575

7676
The `state` API cannot be used for primitive values because:
7777

@@ -84,24 +84,24 @@ The simple solution is wrapping the value in an object wrapper that can be passe
8484
``` js
8585
import { value } from 'vue'
8686

87-
const countRef = value(0)
87+
const countPointer = value(0)
8888
```
8989

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

9292
``` js
9393
// read the value
94-
console.log(countRef.value) // 0
94+
console.log(countPointer.value) // 0
9595

9696
// mutate the value
97-
countRef.value++
97+
countPointer.value++
9898
```
9999

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

102-
## Computed Refs
102+
## Computed Pointers
103103

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

106106
``` js
107107
import { value, computed } from 'vue'
@@ -114,12 +114,12 @@ count.value++
114114
console.log(countPlusOne.value) // 2
115115
```
116116

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

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

121121
``` js
122-
const writableRef = computed(
122+
const writablePointer = computed(
123123
// read
124124
() => count.value + 1,
125125
// write
@@ -129,7 +129,7 @@ const writableRef = computed(
129129
)
130130
```
131131

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

134134
## Watchers
135135

@@ -174,15 +174,15 @@ count.value++
174174
// -> count + 1 is: 2
175175
```
176176

177-
### Watching Refs
177+
### Watching Pointers
178178

179-
The 1st argument can also be a ref:
179+
The 1st argument can also be a pointer:
180180

181181
``` js
182-
// double is a computed ref
182+
// double is a computed pointer
183183
const double = computed(() => count.value * 2)
184184

185-
// watch a ref directly
185+
// watch a pointer directly
186186
watch(double, value => {
187187
console.log('double the count is: ', value)
188188
})
@@ -224,7 +224,7 @@ The **effect** callback can also return a cleanup function which gets called eve
224224
- the watcher is stopped
225225

226226
``` js
227-
watch(idRef, id => {
227+
watch(idPointer, id => {
228228
const token = performAsyncOperation(id)
229229

230230
return () => {
@@ -249,11 +249,11 @@ watch(
249249
)
250250
```
251251

252-
## Exposing Refs to Components
252+
## Exposing Pointers to Components
253253

254254
While this proposal is focused on working with reactive state outside of components, such state should also be usable inside components as well.
255255

256-
Refs can be returned in a component's `data()` function:
256+
Pointers can be returned in a component's `data()` function:
257257

258258
``` js
259259
import { value } from 'vue'
@@ -267,7 +267,7 @@ export default {
267267
}
268268
```
269269

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

272272
``` html
273273
<div @click="count++">
@@ -281,7 +281,7 @@ The APIs proposed here are just low-level building blocks. Technically, they pro
281281

282282
# Drawbacks
283283

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

286286
# Alternatives
287287

@@ -299,4 +299,4 @@ This is mostly new APIs that expose existing internal capabilities. Users famili
299299

300300
Sidenote: removing `this.$watch` and the `watch` option also makes the entire `watch` API completely tree-shakable.
301301

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

Comments
 (0)