Skip to content

Commit 313e4bf

Browse files
committed
fix(reactivity): avoid infinite recursion when mutating ref wrapped in reactive
close #11696
1 parent 9c4c2e5 commit 313e4bf

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/reactivity/__tests__/reactive.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,13 @@ describe('reactivity/reactive', () => {
382382
count++
383383
}
384384
})
385+
386+
// #11696
387+
test('should use correct receiver on set handler for refs', () => {
388+
const a = reactive(ref(1))
389+
effect(() => a.value)
390+
expect(() => {
391+
a.value++
392+
}).not.toThrow()
393+
})
385394
})

packages/reactivity/src/baseHandlers.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ class MutableReactiveHandler extends BaseReactiveHandler {
165165
isArray(target) && isIntegerKey(key)
166166
? Number(key) < target.length
167167
: hasOwn(target, key)
168-
const result = Reflect.set(target, key, value, receiver)
168+
const result = Reflect.set(
169+
target,
170+
key,
171+
value,
172+
isRef(target) ? target : receiver,
173+
)
169174
// don't trigger if target is something up in the prototype chain of original
170175
if (target === toRaw(receiver)) {
171176
if (!hadKey) {

0 commit comments

Comments
 (0)