Skip to content

Commit

Permalink
fix(runtime-core): handle shallow reactive arrays in renderList corre…
Browse files Browse the repository at this point in the history
…ctly (#11870)

close #11869
  • Loading branch information
jh-leong authored Sep 10, 2024
1 parent 67d6596 commit ced59ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/runtime-core/__tests__/helpers/renderList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isReactive, reactive, shallowReactive } from '../../src/index'
import { renderList } from '../../src/helpers/renderList'

describe('renderList', () => {
Expand Down Expand Up @@ -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])
})
})
11 changes: 9 additions & 2 deletions packages/runtime-core/src/helpers/renderList.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit ced59ab

Please sign in to comment.