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(runtime-core): ref in v-for does not trigger watchEffect #6169

Closed
wants to merge 2 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/runtime-core/__tests__/rendererTemplateRef.spec.ts
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
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