Skip to content

Commit edf2638

Browse files
authoredJun 7, 2024··
fix(runtime-core): fix stale v-memo after v-if toggle (#6606)
close #6593
1 parent 293cf4e commit edf2638

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed
 

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

+11
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ describe('v-memo', () => {
148148
// should update
149149
await nextTick()
150150
expect(el.innerHTML).toBe(`<div>3 3</div>`)
151+
152+
vm.ok = true
153+
await nextTick()
154+
vm.ok = false
155+
await nextTick()
156+
expect(el.innerHTML).toBe(`<div>3 3</div>`)
157+
158+
vm.y++
159+
// should update
160+
await nextTick()
161+
expect(el.innerHTML).toBe(`<div>4 3</div>`)
151162
})
152163

153164
test('on v-for', async () => {

‎packages/runtime-core/src/component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ export interface ComponentInternalInstance {
322322
* after initialized (e.g. inline handlers)
323323
* @internal
324324
*/
325-
renderCache: (Function | VNode)[]
325+
renderCache: (Function | VNode | undefined)[]
326326

327327
/**
328328
* Resolved component registry, only for components with mixins or extends

‎packages/runtime-core/src/helpers/withMemo.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export function withMemo(
1515

1616
// shallow clone
1717
ret.memo = memo.slice()
18+
ret.memoIndex = index
19+
1820
return (cache[index] = ret)
1921
}
2022

‎packages/runtime-core/src/renderer.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2110,12 +2110,18 @@ function baseCreateRenderer(
21102110
shapeFlag,
21112111
patchFlag,
21122112
dirs,
2113+
memoIndex,
21132114
} = vnode
21142115
// unset ref
21152116
if (ref != null) {
21162117
setRef(ref, null, parentSuspense, vnode, true)
21172118
}
21182119

2120+
// #6593 should clean memo cache when unmount
2121+
if (memoIndex != null) {
2122+
parentComponent!.renderCache[memoIndex] = undefined
2123+
}
2124+
21192125
if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
21202126
;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode)
21212127
return

‎packages/runtime-core/src/vnode.ts

+4
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ export interface VNode<
228228
* @internal attached by v-memo
229229
*/
230230
memo?: any[]
231+
/**
232+
* @internal index for cleaning v-memo cache
233+
*/
234+
memoIndex?: number
231235
/**
232236
* @internal __COMPAT__ only
233237
*/

0 commit comments

Comments
 (0)
Please sign in to comment.