Skip to content

Commit 3470308

Browse files
authored
fix(reactivity): ensure readonly on plain arrays doesn't track array methods. (#2506)
fix #2493
1 parent 53f4885 commit 3470308

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/reactivity/__tests__/readonly.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ describe('reactivity/readonly', () => {
375375
expect(dummy).toBe(1)
376376
})
377377

378+
test('readonly array should not track', () => {
379+
const arr = [1]
380+
const roArr = readonly(arr)
381+
382+
const eff = effect(() => {
383+
roArr.includes(2)
384+
})
385+
expect(eff.deps.length).toBe(0)
386+
})
387+
378388
test('readonly should track and trigger if wrapping reactive original (collection)', () => {
379389
const a = reactive(new Map())
380390
const b = readonly(a)

packages/reactivity/src/baseHandlers.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ function createGetter(isReadonly = false, shallow = false) {
8383
}
8484

8585
const targetIsArray = isArray(target)
86-
if (targetIsArray && hasOwn(arrayInstrumentations, key)) {
86+
87+
if (!isReadonly && targetIsArray && hasOwn(arrayInstrumentations, key)) {
8788
return Reflect.get(arrayInstrumentations, key, receiver)
8889
}
8990

0 commit comments

Comments
 (0)