mirror of https://github.com/vuejs/core.git
fix(runtime-core): handle shallow reactive arrays in renderList correctly (#11870)
close #11869
This commit is contained in:
parent
67d6596d40
commit
ced59ab8f2
|
@ -1,3 +1,4 @@
|
||||||
|
import { isReactive, reactive, shallowReactive } from '../../src/index'
|
||||||
import { renderList } from '../../src/helpers/renderList'
|
import { renderList } from '../../src/helpers/renderList'
|
||||||
|
|
||||||
describe('renderList', () => {
|
describe('renderList', () => {
|
||||||
|
@ -56,4 +57,12 @@ describe('renderList', () => {
|
||||||
renderList(undefined, (item, index) => `node ${index}: ${item}`),
|
renderList(undefined, (item, index) => `node ${index}: ${item}`),
|
||||||
).toEqual([])
|
).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])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
import type { VNode, VNodeChild } from '../vnode'
|
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 { isArray, isObject, isString } from '@vue/shared'
|
||||||
import { warn } from '../warning'
|
import { warn } from '../warning'
|
||||||
|
|
||||||
|
@ -63,13 +68,15 @@ export function renderList(
|
||||||
|
|
||||||
if (sourceIsArray || isString(source)) {
|
if (sourceIsArray || isString(source)) {
|
||||||
const sourceIsReactiveArray = sourceIsArray && isReactive(source)
|
const sourceIsReactiveArray = sourceIsArray && isReactive(source)
|
||||||
|
let needsWrap = false
|
||||||
if (sourceIsReactiveArray) {
|
if (sourceIsReactiveArray) {
|
||||||
|
needsWrap = !isShallow(source)
|
||||||
source = shallowReadArray(source)
|
source = shallowReadArray(source)
|
||||||
}
|
}
|
||||||
ret = new Array(source.length)
|
ret = new Array(source.length)
|
||||||
for (let i = 0, l = source.length; i < l; i++) {
|
for (let i = 0, l = source.length; i < l; i++) {
|
||||||
ret[i] = renderItem(
|
ret[i] = renderItem(
|
||||||
sourceIsReactiveArray ? toReactive(source[i]) : source[i],
|
needsWrap ? toReactive(source[i]) : source[i],
|
||||||
i,
|
i,
|
||||||
undefined,
|
undefined,
|
||||||
cached && cached[i],
|
cached && cached[i],
|
||||||
|
|
Loading…
Reference in New Issue