Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/reactivity/__tests__/ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,4 +467,23 @@ describe('reactivity/ref', () => {
expect(toValue(c)).toBe(3)
expect(toValue(d)).toBe(4)
})

// #11532
test('ref should be unwrapped when getting the value', () => {
const a = ref(0)
const b = ref()
b.value = a
expect(b.value).toBe(0)

b.value++
expect(b.value).toBe(1)
expect(a.value).toBe(1)

b.value = ref(2)
expect(b.value).toBe(2)

b.value++
expect(b.value).toBe(3)
expect(a.value).toBe(1)
})
})
23 changes: 13 additions & 10 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ function createRef(rawValue: unknown, shallow: boolean) {
class RefImpl<T> {
private _value: T
private _rawValue: T

public dep?: Dep = undefined
public readonly __v_isRef = true

Expand All @@ -172,18 +171,22 @@ class RefImpl<T> {

get value() {
trackRefValue(this)
return this._value
return unref(this._value)
}

set value(newVal) {
const useDirectValue =
this.__v_isShallow || isShallow(newVal) || isReadonly(newVal)
newVal = useDirectValue ? newVal : toRaw(newVal)
if (hasChanged(newVal, this._rawValue)) {
const oldVal = this._rawValue
this._rawValue = newVal
this._value = useDirectValue ? newVal : toReactive(newVal)
triggerRefValue(this, DirtyLevels.Dirty, newVal, oldVal)
if (!isRef(newVal) && isRef(this._rawValue)) {
this._rawValue.value = newVal
} else {
const useDirectValue =
this.__v_isShallow || isShallow(newVal) || isReadonly(newVal)
newVal = useDirectValue ? newVal : toRaw(newVal)
if (hasChanged(newVal, this._rawValue)) {
const oldVal = this._rawValue
this._rawValue = newVal
this._value = useDirectValue ? newVal : toReactive(newVal)
triggerRefValue(this, DirtyLevels.Dirty, newVal, oldVal)
}
}
}
}
Expand Down