Skip to content

Commit 7f9401e

Browse files
feat: add unwrap example code to reactive (#814)
* feat: add unwrap example code to reactive * chore: add change the warning to a tip and add a warning * Apply suggestions from code review Co-authored-by: Natalia Tepluhina <tarya.se@gmail.com> Co-authored-by: Natalia Tepluhina <tarya.se@gmail.com>
1 parent 27ca5bd commit 7f9401e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/api/basic-reactivity.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,44 @@ The reactive conversion is "deep"—it affects all nested properties. In the [ES
1818
function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
1919
```
2020

21+
::: tip Note
22+
`reactive` will unwrap all the deep [refs](./refs-api.html#ref), while maintaining the ref reactivity
23+
24+
```ts
25+
const count = ref(1)
26+
const obj = reactive({ count })
27+
28+
// ref will be unwrapped
29+
console.log(obj.count === count.value) // true
30+
31+
// it will update `obj.value`
32+
count.value++
33+
console.log(count.value) // 2
34+
console.log(obj.count) // 2
35+
36+
// it will also update `count` ref
37+
obj.count++
38+
console.log(obj.count) // 3
39+
console.log(count.value) // 3
40+
```
41+
42+
:::
43+
44+
::: warning Important
45+
When assigning a [ref](./refs-api.html#ref) to a `reactive` property, that ref will be automatically unwrapped.
46+
47+
```ts
48+
const count = ref(1)
49+
const obj = reactive({})
50+
51+
obj.count = count
52+
53+
console.log(obj.count) // 1
54+
console.log(obj.count === count.value) // true
55+
```
56+
57+
:::
58+
2159
## `readonly`
2260

2361
Takes an object (reactive or plain) or a [ref](./refs-api.html#ref) and returns a readonly proxy to the original. A readonly proxy is deep: any nested property accessed will be readonly as well.

0 commit comments

Comments
 (0)