Skip to content

Commit 7b1b7cf

Browse files
committed
fix(runtime-vapor): fix readonly warning when useTemplateRef has same variable name as template ref
close #13665
1 parent 56a7f9d commit 7b1b7cf

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

packages/runtime-core/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,7 @@ export { initFeatureFlags } from './featureFlags'
562562
* @internal
563563
*/
564564
export { createInternalObject } from './internalObject'
565+
/**
566+
* @internal
567+
*/
568+
export { validateTemplateRef } from './rendererTemplateRef'

packages/runtime-core/src/rendererTemplateRef.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import { warn } from './warning'
1414
import { isRef, toRaw } from '@vue/reactivity'
1515
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
1616
import { queuePostRenderEffect } from './renderer'
17-
import { type ComponentOptions, getComponentPublicInstance } from './component'
17+
import {
18+
type ComponentOptions,
19+
type Data,
20+
getComponentPublicInstance,
21+
} from './component'
1822
import { knownTemplateRefs } from './helpers/useTemplateRef'
1923

2024
/**
@@ -73,25 +77,7 @@ export function setRef(
7377
const oldRef = oldRawRef && (oldRawRef as VNodeNormalizedRefAtom).r
7478
const refs = owner.refs === EMPTY_OBJ ? (owner.refs = {}) : owner.refs
7579
const setupState = owner.setupState
76-
const rawSetupState = toRaw(setupState)
77-
const canSetSetupRef =
78-
setupState === EMPTY_OBJ
79-
? () => false
80-
: (key: string) => {
81-
if (__DEV__) {
82-
if (hasOwn(rawSetupState, key) && !isRef(rawSetupState[key])) {
83-
warn(
84-
`Template ref "${key}" used on a non-ref value. ` +
85-
`It will not work in the production build.`,
86-
)
87-
}
88-
89-
if (knownTemplateRefs.has(rawSetupState[key] as any)) {
90-
return false
91-
}
92-
}
93-
return hasOwn(rawSetupState, key)
94-
}
80+
const canSetSetupRef = validateTemplateRef(setupState)
9581

9682
// dynamic ref changed. unset old ref
9783
if (oldRef != null && oldRef !== ref) {
@@ -161,3 +147,26 @@ export function setRef(
161147
}
162148
}
163149
}
150+
151+
export function validateTemplateRef(
152+
setupState: Data,
153+
): (key: string) => boolean {
154+
const rawSetupState = toRaw(setupState)
155+
return setupState === EMPTY_OBJ
156+
? () => false
157+
: (key: string) => {
158+
if (__DEV__) {
159+
if (hasOwn(rawSetupState, key) && !isRef(rawSetupState[key])) {
160+
warn(
161+
`Template ref "${key}" used on a non-ref value. ` +
162+
`It will not work in the production build.`,
163+
)
164+
}
165+
166+
if (knownTemplateRefs.has(rawSetupState[key] as any)) {
167+
return false
168+
}
169+
}
170+
return hasOwn(rawSetupState, key)
171+
}
172+
}

packages/runtime-vapor/src/apiTemplateRef.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type SchedulerJob,
1111
callWithErrorHandling,
1212
queuePostFlushCb,
13+
validateTemplateRef,
1314
warn,
1415
} from '@vue/runtime-dom'
1516
import {
@@ -55,6 +56,7 @@ export function setRef(
5556
const refs =
5657
instance.refs === EMPTY_OBJ ? (instance.refs = {}) : instance.refs
5758

59+
const canSetSetupRef = validateTemplateRef(setupState)
5860
// dynamic ref changed. unset old ref
5961
if (oldRef != null && oldRef !== ref) {
6062
if (isString(oldRef)) {
@@ -87,7 +89,7 @@ export function setRef(
8789
const doSet: SchedulerJob = () => {
8890
if (refFor) {
8991
existing = _isString
90-
? __DEV__ && hasOwn(setupState, ref)
92+
? __DEV__ && canSetSetupRef(ref)
9193
? setupState[ref]
9294
: refs[ref]
9395
: ref.value
@@ -96,7 +98,7 @@ export function setRef(
9698
existing = [refValue]
9799
if (_isString) {
98100
refs[ref] = existing
99-
if (__DEV__ && hasOwn(setupState, ref)) {
101+
if (__DEV__ && canSetSetupRef(ref)) {
100102
setupState[ref] = refs[ref]
101103
// if setupState[ref] is a reactivity ref,
102104
// the existing will also become reactivity too
@@ -111,7 +113,7 @@ export function setRef(
111113
}
112114
} else if (_isString) {
113115
refs[ref] = refValue
114-
if (__DEV__ && hasOwn(setupState, ref)) {
116+
if (__DEV__ && canSetSetupRef(ref)) {
115117
setupState[ref] = refValue
116118
}
117119
} else if (_isRef) {
@@ -129,7 +131,7 @@ export function setRef(
129131
remove(existing, refValue)
130132
} else if (_isString) {
131133
refs[ref] = null
132-
if (__DEV__ && hasOwn(setupState, ref)) {
134+
if (__DEV__ && canSetSetupRef(ref)) {
133135
setupState[ref] = null
134136
}
135137
} else if (_isRef) {

0 commit comments

Comments
 (0)