Skip to content

Commit cee1eaf

Browse files
authoredApr 12, 2022
fix(runtime-core/template-ref): named ref in v-for regression fix (#5118)
close #5116 close #5447 close #5525
1 parent 7efb9db commit cee1eaf

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
 

‎packages/runtime-core/__tests__/rendererTemplateRef.spec.ts

+55
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,59 @@ describe('api: template refs', () => {
442442
await nextTick()
443443
expect(mapRefs()).toMatchObject(['2', '3', '4'])
444444
})
445+
446+
447+
448+
test('named ref in v-for', async () => {
449+
const show = ref(true);
450+
const list = reactive([1, 2, 3])
451+
const listRefs = ref([])
452+
const mapRefs = () => listRefs.value.map(n => serializeInner(n))
453+
454+
const App = {
455+
setup() {
456+
return { listRefs }
457+
},
458+
render() {
459+
return show.value
460+
? h(
461+
'ul',
462+
list.map(i =>
463+
h(
464+
'li',
465+
{
466+
ref: 'listRefs',
467+
ref_for: true
468+
},
469+
i
470+
)
471+
)
472+
)
473+
: null
474+
}
475+
}
476+
const root = nodeOps.createElement('div')
477+
render(h(App), root)
478+
479+
expect(mapRefs()).toMatchObject(['1', '2', '3'])
480+
481+
list.push(4)
482+
await nextTick()
483+
expect(mapRefs()).toMatchObject(['1', '2', '3', '4'])
484+
485+
list.shift()
486+
await nextTick()
487+
expect(mapRefs()).toMatchObject(['2', '3', '4'])
488+
489+
show.value = !show.value
490+
await nextTick()
491+
492+
expect(mapRefs()).toMatchObject([])
493+
494+
show.value = !show.value
495+
await nextTick()
496+
expect(mapRefs()).toMatchObject(['2', '3', '4'])
497+
})
498+
499+
445500
})

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

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ export function setRef(
9191
if (!isArray(existing)) {
9292
if (_isString) {
9393
refs[ref] = [refValue]
94+
if (hasOwn(setupState, ref)) {
95+
setupState[ref] = refs[ref]
96+
}
9497
} else {
9598
ref.value = [refValue]
9699
if (rawRef.k) refs[rawRef.k] = ref.value

0 commit comments

Comments
 (0)
Please sign in to comment.