Skip to content

Commit

Permalink
chore(runtime-core): warn if use a non-ref to hold the element refere…
Browse files Browse the repository at this point in the history
…nce in DEV
  • Loading branch information
edison1105 committed Sep 27, 2024
1 parent aa9ef23 commit 8a5e701
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ describe('api: template refs', () => {
}
render(h(Comp), root)
expect(state.refKey).toBe(root.children[0])
expect('refKey is not a ref').toHaveBeenWarned()
})

test('multiple root refs', () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/runtime-core/src/rendererTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,16 @@ export function setRef(
setupState === EMPTY_OBJ
? () => false
: (key: string) => {
if (__DEV__ && knownTemplateRefs.has(rawSetupState[key] as any)) {
return false
if (__DEV__) {
if (hasOwn(rawSetupState, key) && !isRef(rawSetupState[key])) {
warn(
`${key} is not a ref. use a ref to hold the element reference is recommended.`,
)
}

if (knownTemplateRefs.has(rawSetupState[key] as any)) {
return false
}
}
return hasOwn(rawSetupState, key)
}
Expand Down

0 comments on commit 8a5e701

Please sign in to comment.