Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): fix error when using Proxy on reactive arrays #9751

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/reactivity/__tests__/reactiveArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ describe('reactivity/reactive/Array', () => {
expect(length).toBe('01')
})

// #9742
test('Array can be proxied', () => {
const array = reactive<number[]>([])
const proxy = new Proxy(array, {})
proxy.push(1)
expect(array).toHaveLength(1)
expect(proxy).toHaveLength(1)
})

test('toRaw on array using reactive as prototype', () => {
const original = reactive([])
const obj = Object.create(original)
const raw = toRaw(obj)
expect(raw).toBe(obj)
expect(raw).not.toBe(toRaw(original))
})

describe('Array methods w/ refs', () => {
let original: any[]
beforeEach(() => {
Expand Down
5 changes: 5 additions & 0 deletions packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ class BaseReactiveHandler implements ProxyHandler<Target> {
}

const targetIsArray = isArray(target)
// #9742 if the target is array prevent and we retriving the RAW, retrieve the correct target
// if receiver is not an array is probably based on prototype.
if (key === ReactiveFlags.RAW && targetIsArray && isArray(receiver)) {
return target
}

if (!isReadonly) {
if (targetIsArray && hasOwn(arrayInstrumentations, key)) {
Expand Down