Skip to content

Commit 3b60358

Browse files
authored
fix(v-memo): should work on v-for with constant expression (#4272)
fix #4246
1 parent c421fb9 commit 3b60358

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/runtime-core/__tests__/helpers/withMemo.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,26 @@ describe('v-memo', () => {
147147
`<div>5 yes z</div><div>2 no z</div><div>3 no z</div>`
148148
)
149149
})
150+
151+
test('on v-for /w constant expression ', async () => {
152+
const [el, vm] = mount({
153+
template: `<div v-for="item in 3" v-memo="[count < 2 ? true : count]">
154+
{{count}}
155+
</div>`,
156+
data: () => ({
157+
count: 0
158+
})
159+
})
160+
expect(el.innerHTML).toBe(`<div>0</div><div>0</div><div>0</div>`)
161+
162+
vm.count = 1
163+
await nextTick()
164+
// should not update
165+
expect(el.innerHTML).toBe(`<div>0</div><div>0</div><div>0</div>`)
166+
167+
vm.count = 2
168+
await nextTick()
169+
// should update
170+
expect(el.innerHTML).toBe(`<div>2</div><div>2</div><div>2</div>`)
171+
})
150172
})

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function renderList(
7171
}
7272
ret = new Array(source)
7373
for (let i = 0; i < source; i++) {
74-
ret[i] = renderItem(i + 1, i)
74+
ret[i] = renderItem(i + 1, i, undefined, cached && cached[i])
7575
}
7676
} else if (isObject(source)) {
7777
if (source[Symbol.iterator as any]) {

0 commit comments

Comments
 (0)