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): don't update setup ref for useTemplateRef key #12756

Open
wants to merge 6 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
22 changes: 22 additions & 0 deletions packages/runtime-core/__tests__/helpers/useTemplateRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
h,
nextTick,
nodeOps,
onMounted,
ref,
render,
useTemplateRef,
Expand Down Expand Up @@ -125,4 +126,25 @@ describe('useTemplateRef', () => {
__DEV__ = true
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}123

})

// #12749
test(`don't update setup ref for useTemplateRef key`, () => {
let foo: ShallowRef
const Comp = {
setup() {
foo = useTemplateRef('bar')
const bar = ref(null)
onMounted(() => {
expect(bar.value).toBe(null)
})
return { bar }
},
render() {
return h('div', { ref: 'bar' })
},
}
const root = nodeOps.createElement('div')
render(h(Comp), root)
expect(foo!.value).toBe(root.children[0])
})
})
16 changes: 9 additions & 7 deletions packages/runtime-core/src/helpers/useTemplateRef.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ShallowRef, readonly, shallowRef } from '@vue/reactivity'
import { getCurrentInstance } from '../component'
import { type Data, getCurrentInstance } from '../component'
import { warn } from '../warning'
import { EMPTY_OBJ } from '@vue/shared'

Expand All @@ -12,12 +12,7 @@ export function useTemplateRef<T = unknown, Keys extends string = string>(
const r = shallowRef(null)
if (i) {
const refs = i.refs === EMPTY_OBJ ? (i.refs = {}) : i.refs
let desc: PropertyDescriptor | undefined
if (
__DEV__ &&
(desc = Object.getOwnPropertyDescriptor(refs, key)) &&
!desc.configurable
) {
if (__DEV__ && isUseTemplateRefKey(refs, key)) {
warn(`useTemplateRef('${key}') already exists.`)
} else {
Object.defineProperty(refs, key, {
Expand All @@ -38,3 +33,10 @@ export function useTemplateRef<T = unknown, Keys extends string = string>(
}
return ret
}

export function isUseTemplateRefKey(refs: Data, key: string): boolean {
let desc: PropertyDescriptor | undefined
return !!(
(desc = Object.getOwnPropertyDescriptor(refs, key)) && !desc.configurable
)
}
11 changes: 10 additions & 1 deletion packages/runtime-core/src/rendererTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import type { SchedulerJob } from './scheduler'
import { queuePostRenderEffect } from './renderer'
import { type ComponentOptions, getComponentPublicInstance } from './component'
import { knownTemplateRefs } from './helpers/useTemplateRef'
import {
isUseTemplateRefKey,
knownTemplateRefs,
} from './helpers/useTemplateRef'

/**
* Function for handling a template ref
Expand Down Expand Up @@ -91,6 +94,12 @@ export function setRef(
return false
}
}

// skip setting up ref if the key is from useTemplateRef
if (isUseTemplateRefKey(refs, key)) {
return false
}

return hasOwn(rawSetupState, key)
}

Expand Down
Loading