Skip to content

Commit

Permalink
fix(runtime-core): handle NaN identity check in v-memo (#5852)
Browse files Browse the repository at this point in the history
fix #5853
  • Loading branch information
hchlq committed May 12, 2022
1 parent d36ca4d commit a388129
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/runtime-core/__tests__/helpers/withMemo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,17 @@ describe('v-memo', () => {
// should update
expect(el.innerHTML).toBe(`<div>2</div><div>2</div><div>2</div>`)
})

test('v-memo dependency is NaN should be equal', async () => {
const [el, vm] = mount({
template: `<div v-memo="[x]">{{ y }}</div>`,
data: () => ({ x: NaN, y: 0 })
})
expect(el.innerHTML).toBe(`<div>0</div>`)

vm.y++
// should not update
await nextTick()
expect(el.innerHTML).toBe(`<div>0</div>`)
})
})
4 changes: 3 additions & 1 deletion packages/runtime-core/src/helpers/withMemo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hasChanged } from '@vue/shared'
import { currentBlock, isBlockTreeEnabled, VNode } from '../vnode'

export function withMemo(
Expand All @@ -22,8 +23,9 @@ export function isMemoSame(cached: VNode, memo: any[]) {
if (prev.length != memo.length) {
return false
}

for (let i = 0; i < prev.length; i++) {
if (prev[i] !== memo[i]) {
if (hasChanged(prev[i], memo[i])) {
return false
}
}
Expand Down

0 comments on commit a388129

Please sign in to comment.