fix(runtime-core): fix stale v-memo after v-if toggle (#6606)

close #6593
This commit is contained in:
edison 2024-06-07 17:29:28 +08:00 committed by GitHub
parent 293cf4e131
commit edf263847e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 24 additions and 1 deletions

View File

@ -148,6 +148,17 @@ describe('v-memo', () => {
// should update // should update
await nextTick() await nextTick()
expect(el.innerHTML).toBe(`<div>3 3</div>`) expect(el.innerHTML).toBe(`<div>3 3</div>`)
vm.ok = true
await nextTick()
vm.ok = false
await nextTick()
expect(el.innerHTML).toBe(`<div>3 3</div>`)
vm.y++
// should update
await nextTick()
expect(el.innerHTML).toBe(`<div>4 3</div>`)
}) })
test('on v-for', async () => { test('on v-for', async () => {

View File

@ -322,7 +322,7 @@ export interface ComponentInternalInstance {
* after initialized (e.g. inline handlers) * after initialized (e.g. inline handlers)
* @internal * @internal
*/ */
renderCache: (Function | VNode)[] renderCache: (Function | VNode | undefined)[]
/** /**
* Resolved component registry, only for components with mixins or extends * Resolved component registry, only for components with mixins or extends

View File

@ -15,6 +15,8 @@ export function withMemo(
// shallow clone // shallow clone
ret.memo = memo.slice() ret.memo = memo.slice()
ret.memoIndex = index
return (cache[index] = ret) return (cache[index] = ret)
} }

View File

@ -2110,12 +2110,18 @@ function baseCreateRenderer(
shapeFlag, shapeFlag,
patchFlag, patchFlag,
dirs, dirs,
memoIndex,
} = vnode } = vnode
// unset ref // unset ref
if (ref != null) { if (ref != null) {
setRef(ref, null, parentSuspense, vnode, true) setRef(ref, null, parentSuspense, vnode, true)
} }
// #6593 should clean memo cache when unmount
if (memoIndex != null) {
parentComponent!.renderCache[memoIndex] = undefined
}
if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) { if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode) ;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode)
return return

View File

@ -228,6 +228,10 @@ export interface VNode<
* @internal attached by v-memo * @internal attached by v-memo
*/ */
memo?: any[] memo?: any[]
/**
* @internal index for cleaning v-memo cache
*/
memoIndex?: number
/** /**
* @internal __COMPAT__ only * @internal __COMPAT__ only
*/ */