Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useTemplateRef): handle useTemplateRef edge case with vFor #12733

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/runtime-core/__tests__/helpers/useTemplateRef.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {
type ShallowRef,
getCurrentInstance,
h,
isReactive,
nextTick,
nodeOps,
ref,
render,
serializeInner,
useTemplateRef,
} from '@vue/runtime-test'

Expand Down Expand Up @@ -70,6 +73,58 @@ describe('useTemplateRef', () => {
expect(t1!.value).toBe(null)
})

// #12731
test('should collect refs as reactive array in v-for', async () => {
let t1: any
const list = ref<number[]>([])
let currentInstance: any
const Comp = {
setup() {
t1 = useTemplateRef('refKey')
currentInstance = getCurrentInstance()!
},
render() {
return h('div', null, [
h('div', null, String(t1.value?.length)),
h(
'ul',
list.value.map(i =>
h(
'li',
{
ref: 'refKey',
ref_for: true,
},
i,
),
),
),
])
},
}
const root = nodeOps.createElement('div')
render(h(Comp), root)
expect(t1!.value).toBe(null)
expect(serializeInner(root)).toBe(
'<div><div>undefined</div><ul></ul></div>',
)

list.value.push(1)
await nextTick()
expect(isReactive(currentInstance.refs['refKey'])).toBe(true)
expect(t1!.value.length).toBe(1)
expect(serializeInner(root)).toBe(
'<div><div>1</div><ul><li>1</li></ul></div>',
)

list.value.push(2)
await nextTick()
expect(t1!.value.length).toBe(2)
expect(serializeInner(root)).toBe(
'<div><div>2</div><ul><li>1</li><li>2</li></ul></div>',
)
})

test('should warn on duplicate useTemplateRef', () => {
const root = nodeOps.createElement('div')
render(
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/rendererTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@vue/shared'
import { isAsyncWrapper } from './apiAsyncComponent'
import { warn } from './warning'
import { isRef, toRaw } from '@vue/reactivity'
import { isRef, shallowReactive, toRaw } from '@vue/reactivity'
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import type { SchedulerJob } from './scheduler'
import { queuePostRenderEffect } from './renderer'
Expand Down Expand Up @@ -125,7 +125,7 @@ export function setRef(
} else {
if (!isArray(existing)) {
if (_isString) {
refs[ref] = [refValue]
refs[ref] = shallowReactive([refValue])
if (canSetSetupRef(ref)) {
setupState[ref] = refs[ref]
}
Expand Down
Loading