Skip to content

Commit

Permalink
fix(reactivity): avoid infinite recursion when mutating ref wrapped i…
Browse files Browse the repository at this point in the history
…n reactive

close #11696
  • Loading branch information
yyx990803 committed Aug 29, 2024
1 parent 9c4c2e5 commit 313e4bf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,13 @@ describe('reactivity/reactive', () => {
count++
}
})

// #11696
test('should use correct receiver on set handler for refs', () => {
const a = reactive(ref(1))
effect(() => a.value)
expect(() => {
a.value++
}).not.toThrow()
})
})
7 changes: 6 additions & 1 deletion packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ class MutableReactiveHandler extends BaseReactiveHandler {
isArray(target) && isIntegerKey(key)
? Number(key) < target.length
: hasOwn(target, key)
const result = Reflect.set(target, key, value, receiver)
const result = Reflect.set(
target,
key,
value,
isRef(target) ? target : receiver,
)
// don't trigger if target is something up in the prototype chain of original
if (target === toRaw(receiver)) {
if (!hadKey) {
Expand Down

0 comments on commit 313e4bf

Please sign in to comment.