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): prevent readonly warning when using useTemplateRef with same variable name as template ref #11802

Closed
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ describe('useTemplateRef', () => {
test('should be readonly', () => {
let tRef
const key = 'refKey'
const key2 = 'foo'
const Comp = {
setup() {
tRef = useTemplateRef(key)
// naming a variable declared with the same as a template ref
const foo = useTemplateRef(key2)
return { [key2]: foo }
},
render() {
return h('div', { ref: key })
return h('div', { ref: key }, [h('div', { ref: key2 })])
},
}
const root = nodeOps.createElement('div')
Expand All @@ -42,7 +46,7 @@ describe('useTemplateRef', () => {
tRef.value = 123

expect(tRef!.value).toBe(root.children[0])
expect('target is readonly').toHaveBeenWarned()
expect('target is readonly').toHaveBeenWarnedTimes(1)
})

test('should be updated for ref of dynamic strings', async () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/runtime-core/src/helpers/useTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { getCurrentInstance } from '../component'
import { warn } from '../warning'
import { EMPTY_OBJ } from '@vue/shared'

export const TEMPLATE_REF_KEYS = '__v_ref_keys'

export function useTemplateRef<T = unknown, Keys extends string = string>(
key: Keys,
): Readonly<ShallowRef<T | null>> {
Expand All @@ -19,6 +21,15 @@ export function useTemplateRef<T = unknown, Keys extends string = string>(
) {
warn(`useTemplateRef('${key}') already exists.`)
} else {
if (refs[TEMPLATE_REF_KEYS]) {
;(refs[TEMPLATE_REF_KEYS] as any).add(key)
} else {
const set = new Set([key])
Object.defineProperty(refs, TEMPLATE_REF_KEYS, {
get: () => set,
})
}

Object.defineProperty(refs, key, {
enumerable: true,
get: () => r.value,
Expand Down
13 changes: 10 additions & 3 deletions packages/runtime-core/src/rendererTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import type { SchedulerJob } from './scheduler'
import { queuePostRenderEffect } from './renderer'
import { getComponentPublicInstance } from './component'
import { TEMPLATE_REF_KEYS } from './helpers/useTemplateRef'

/**
* Function for handling a template ref
Expand Down Expand Up @@ -64,11 +65,17 @@ export function setRef(
const refs = owner.refs === EMPTY_OBJ ? (owner.refs = {}) : owner.refs
const setupState = owner.setupState

const canUpdateSetupState = (ref: string) => {
if (!hasOwn(setupState, ref)) return false
const set: Set<string> | undefined = refs[TEMPLATE_REF_KEYS] as any
return !set || !set.has(ref)
}

// dynamic ref changed. unset old ref
if (oldRef != null && oldRef !== ref) {
if (isString(oldRef)) {
refs[oldRef] = null
if (hasOwn(setupState, oldRef)) {
if (canUpdateSetupState(oldRef)) {
setupState[oldRef] = null
}
} else if (isRef(oldRef)) {
Expand All @@ -95,7 +102,7 @@ export function setRef(
if (!isArray(existing)) {
if (_isString) {
refs[ref] = [refValue]
if (hasOwn(setupState, ref)) {
if (canUpdateSetupState(ref)) {
setupState[ref] = refs[ref]
}
} else {
Expand All @@ -108,7 +115,7 @@ export function setRef(
}
} else if (_isString) {
refs[ref] = value
if (hasOwn(setupState, ref)) {
if (canUpdateSetupState(ref)) {
setupState[ref] = value
}
} else if (_isRef) {
Expand Down