Skip to content

Commit

Permalink
fix(runtime-core): fix stale v-memo after v-if toggle (#6606)
Browse files Browse the repository at this point in the history
close #6593
  • Loading branch information
edison1105 authored Jun 7, 2024
1 parent 293cf4e commit edf2638
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/runtime-core/__tests__/helpers/withMemo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ describe('v-memo', () => {
// should update
await nextTick()
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 () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export interface ComponentInternalInstance {
* after initialized (e.g. inline handlers)
* @internal
*/
renderCache: (Function | VNode)[]
renderCache: (Function | VNode | undefined)[]

/**
* Resolved component registry, only for components with mixins or extends
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/helpers/withMemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export function withMemo(

// shallow clone
ret.memo = memo.slice()
ret.memoIndex = index

return (cache[index] = ret)
}

Expand Down
6 changes: 6 additions & 0 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2110,12 +2110,18 @@ function baseCreateRenderer(
shapeFlag,
patchFlag,
dirs,
memoIndex,
} = vnode
// unset ref
if (ref != null) {
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) {
;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode)
return
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ export interface VNode<
* @internal attached by v-memo
*/
memo?: any[]
/**
* @internal index for cleaning v-memo cache
*/
memoIndex?: number
/**
* @internal __COMPAT__ only
*/
Expand Down

0 comments on commit edf2638

Please sign in to comment.