]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): handle shallow reactive arrays in renderList correctly (#11870)
authorTycho <jh.leong@outlook.com>
Tue, 10 Sep 2024 07:48:14 +0000 (15:48 +0800)
committerGitHub <noreply@github.com>
Tue, 10 Sep 2024 07:48:14 +0000 (15:48 +0800)
close #11869

packages/runtime-core/__tests__/helpers/renderList.spec.ts
packages/runtime-core/src/helpers/renderList.ts

index d23a63cc08e02e31fc925b3d8b25a5567e75d965..8dd04ff8a9b4d5a7e7eaf5165f093885e6ff1468 100644 (file)
@@ -1,3 +1,4 @@
+import { isReactive, reactive, shallowReactive } from '../../src/index'
 import { renderList } from '../../src/helpers/renderList'
 
 describe('renderList', () => {
@@ -56,4 +57,12 @@ describe('renderList', () => {
       renderList(undefined, (item, index) => `node ${index}: ${item}`),
     ).toEqual([])
   })
+
+  it('should render items in a reactive array correctly', () => {
+    const reactiveArray = reactive([{ foo: 1 }])
+    expect(renderList(reactiveArray, isReactive)).toEqual([true])
+
+    const shallowReactiveArray = shallowReactive([{ foo: 1 }])
+    expect(renderList(shallowReactiveArray, isReactive)).toEqual([false])
+  })
 })
index 12b0317bdbdb1ac371312cb52e2526f611b3a416..bbcbcc13044330fed58c776f684ece2ff837c0b5 100644 (file)
@@ -1,5 +1,10 @@
 import type { VNode, VNodeChild } from '../vnode'
-import { isReactive, shallowReadArray, toReactive } from '@vue/reactivity'
+import {
+  isReactive,
+  isShallow,
+  shallowReadArray,
+  toReactive,
+} from '@vue/reactivity'
 import { isArray, isObject, isString } from '@vue/shared'
 import { warn } from '../warning'
 
@@ -63,13 +68,15 @@ export function renderList(
 
   if (sourceIsArray || isString(source)) {
     const sourceIsReactiveArray = sourceIsArray && isReactive(source)
+    let needsWrap = false
     if (sourceIsReactiveArray) {
+      needsWrap = !isShallow(source)
       source = shallowReadArray(source)
     }
     ret = new Array(source.length)
     for (let i = 0, l = source.length; i < l; i++) {
       ret[i] = renderItem(
-        sourceIsReactiveArray ? toReactive(source[i]) : source[i],
+        needsWrap ? toReactive(source[i]) : source[i],
         i,
         undefined,
         cached && cached[i],