Skip to content
Closed
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
58 changes: 58 additions & 0 deletions packages/runtime-core/__tests__/rendererTemplateRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
serializeInner,
shallowRef
} from '@vue/runtime-test'
import { watchEffect } from '@vue/runtime-core'

// reference: https://vue-composition-api-rfc.netlify.com/api.html#template-refs

Expand Down Expand Up @@ -493,4 +494,61 @@ describe('api: template refs', () => {
await nextTick()
expect(mapRefs()).toMatchObject(['2', '3', '4'])
})

test('named ref in v-for with watchEffect', async () => {
const show = ref(true)
const list = reactive([1, 2, 3])
const listRefs = ref([])
const check = ref()

const mapRefs = () => listRefs.value.map(n => serializeInner(n))
watchEffect(() => (check.value = mapRefs()))

const App = {
setup() {
return { listRefs }
},
render() {
return show.value
? h(
'ul',
list.map(i =>
h(
'li',
{
ref: 'listRefs',
ref_for: true
},
i
)
)
)
: null
}
}
const root = nodeOps.createElement('div')
render(h(App), root)

expect(check.value).toMatchObject([])

await nextTick()
expect(check.value).toMatchObject(['1', '2', '3'])

list.push(4)
await nextTick()
expect(check.value).toMatchObject(['1', '2', '3', '4'])

list.shift()
await nextTick()
expect(check.value).toMatchObject(['2', '3', '4'])

show.value = !show.value
await nextTick()

expect(check.value).toMatchObject([])

show.value = !show.value
await nextTick()
expect(check.value).toMatchObject(['2', '3', '4'])
})
})
2 changes: 1 addition & 1 deletion packages/runtime-core/src/rendererTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function setRef(
if (_isString || _isRef) {
const doSet = () => {
if (rawRef.f) {
const existing = _isString ? refs[ref] : ref.value
const existing = _isString ? hasOwn(setupState, ref) ? setupState[ref] : refs[ref] : ref.value
if (isUnmount) {
isArray(existing) && remove(existing, refValue)
} else {
Expand Down