Skip to content

Commit 3c05f8b

Browse files
authored
fix(reactivity): unwrap non-index accessed refs on reactive arrays (#1859)
close #1846
1 parent 191080b commit 3c05f8b

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

packages/reactivity/__tests__/ref.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ describe('reactivity/ref', () => {
115115
expect((arr[1] as Ref).value).toBe(3)
116116
})
117117

118+
it('should unwrap ref types as props of arrays', () => {
119+
const arr = [ref(0)]
120+
const symbolKey = Symbol('')
121+
arr['' as any] = ref(1)
122+
arr[symbolKey as any] = ref(2)
123+
const arrRef = ref(arr).value
124+
expect(isRef(arrRef[0])).toBe(true)
125+
expect(isRef(arrRef['' as any])).toBe(false)
126+
expect(isRef(arrRef[symbolKey as any])).toBe(false)
127+
expect(arrRef['' as any]).toBe(1)
128+
expect(arrRef[symbolKey as any]).toBe(2)
129+
})
130+
118131
it('should keep tuple types', () => {
119132
const tuple: [number, string, { a: number }, () => number, Ref<number>] = [
120133
0,

packages/reactivity/src/baseHandlers.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ function createGetter(isReadonly = false, shallow = false) {
6363

6464
const res = Reflect.get(target, key, receiver)
6565

66+
const keyIsSymbol = isSymbol(key)
6667
if (
67-
isSymbol(key)
68-
? builtInSymbols.has(key)
68+
keyIsSymbol
69+
? builtInSymbols.has(key as symbol)
6970
: key === `__proto__` || key === `__v_isRef`
7071
) {
7172
return res
@@ -80,8 +81,12 @@ function createGetter(isReadonly = false, shallow = false) {
8081
}
8182

8283
if (isRef(res)) {
83-
// ref unwrapping, only for Objects, not for Arrays.
84-
return targetIsArray ? res : res.value
84+
// ref unwrapping - does not apply for Array + integer key.
85+
const shouldUnwrap =
86+
!targetIsArray ||
87+
keyIsSymbol ||
88+
'' + parseInt(key as string, 10) !== key
89+
return shouldUnwrap ? res.value : res
8590
}
8691

8792
if (isObject(res)) {

0 commit comments

Comments
 (0)