Skip to content

Commit 08f504c

Browse files
authored
fix(ref): should not trigger when setting value to same proxy (#3658)
1 parent f6a5f09 commit 08f504c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

packages/reactivity/__tests__/ref.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,24 @@ describe('reactivity/ref', () => {
336336
_trigger!()
337337
expect(dummy).toBe(2)
338338
})
339+
340+
test('should not trigger when setting value to same proxy', () => {
341+
const obj = reactive({ count: 0 })
342+
343+
const a = ref(obj)
344+
const spy1 = jest.fn(() => a.value)
345+
346+
effect(spy1)
347+
348+
a.value = obj
349+
expect(spy1).toBeCalledTimes(1)
350+
351+
const b = shallowRef(obj)
352+
const spy2 = jest.fn(() => b.value)
353+
354+
effect(spy2)
355+
356+
b.value = obj
357+
expect(spy2).toBeCalledTimes(1)
358+
})
339359
})

packages/reactivity/src/ref.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ export function shallowRef(value?: unknown) {
5252
}
5353

5454
class RefImpl<T> {
55+
private _rawValue: T
56+
5557
private _value: T
5658

5759
public readonly __v_isRef = true
5860

59-
constructor(private _rawValue: T, public readonly _shallow: boolean) {
60-
this._value = _shallow ? _rawValue : convert(_rawValue)
61+
constructor(value: T, public readonly _shallow = false) {
62+
this._rawValue = _shallow ? value : toRaw(value)
63+
this._value = _shallow ? value : convert(value)
6164
}
6265

6366
get value() {
@@ -66,7 +69,8 @@ class RefImpl<T> {
6669
}
6770

6871
set value(newVal) {
69-
if (hasChanged(toRaw(newVal), this._rawValue)) {
72+
newVal = this._shallow ? newVal : toRaw(newVal)
73+
if (hasChanged(newVal, this._rawValue)) {
7074
this._rawValue = newVal
7175
this._value = this._shallow ? newVal : convert(newVal)
7276
trigger(toRaw(this), TriggerOpTypes.SET, 'value', newVal)

0 commit comments

Comments
 (0)