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): setRef should not update setupState's TemplateRef #11810

Closed
wants to merge 15 commits into from

Conversation

linzhe141
Copy link
Contributor

@linzhe141 linzhe141 commented Sep 4, 2024

close #11808
close #11816

Copy link

github-actions bot commented Sep 4, 2024

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 100 kB (-3 B) 37.7 kB (+9 B) 34 kB (-40 B)
vue.global.prod.js 159 kB (-3 B) 57.6 kB (+4 B) 51.2 kB (+4 B)

Usages

Name Size Gzip Brotli
createApp 55.2 kB (-25 B) 21.2 kB (-10 B) 19.4 kB (+60 B)
createSSRApp 59.2 kB (-25 B) 22.9 kB (-5 B) 20.9 kB (+13 B)
defineCustomElement 59.9 kB (-25 B) 22.8 kB (-7 B) 20.7 kB (-53 B)
overall 69 kB (-25 B) 26.3 kB (-7 B) 23.9 kB (-74 B)

Copy link

pkg-pr-new bot commented Sep 4, 2024

Open in Stackblitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@11810

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@11810

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@11810

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@11810

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@11810

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@11810

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@11810

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@11810

vue

pnpm add https://pkg.pr.new/vue@11810

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@11810

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@11810

commit: 3bc3c1d

@edison1105 edison1105 added the need test The PR has missing test cases. label Sep 5, 2024
@linzhe141 linzhe141 changed the title fix(runtime-core): setRef should not update TemplateRef fix(runtime-core): setRef should not update setupState's TemplateRef Sep 5, 2024
Copy link
Member

@baiwusanyu-c baiwusanyu-c left a comment

Choose a reason for hiding this comment

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

I'm wondering if this usage is correct. The information given in the documentation seems to be more inclined to the ref binding string being passed as a parameter to useTemplateRef, rather than binding the return value of useTemplateRef to the ref attribute.

const inputRef = useTemplateRef('my-ref')
<input ref="my-ref"/>
const inputRef = useTemplateRef('my-ref')
<input ref="inputRef"/>

https://vuejs.org/guide/essentials/template-refs.html#accessing-the-refs

wdyt ? @edison1105

@@ -940,6 +940,11 @@ export function handleSetupResult(
}
instance.setupState = proxyRefs(setupResult)
if (__DEV__) {
// dev only
Object.defineProperty(setupResult, '__v__setupResult', {
enumerable: false,
Copy link
Member

Choose a reason for hiding this comment

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

the default value of enumerable is false. so this line is unnecessary.

return {
[key]: tRef,
['foo']: foo,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
['foo']: foo,
foo,

@@ -8,6 +8,8 @@ export function useTemplateRef<T = unknown, Keys extends string = string>(
): Readonly<ShallowRef<T | null>> {
const i = getCurrentInstance()
const r = shallowRef(null)
// @ts-expect-error
r.__v__TemplateRef = true
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
r.__v__TemplateRef = true
if(__DEV__) r.__v__TemplateRef = true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to modify it like this before, but it caused the problem mentioned in #11816

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or I could modify it like this, but I'm not sure which way is more reasonable.
image

I also think this is a very extreme use case, maybe it's just wrong, but I still think we should handle this extreme situation.

@edison1105
Copy link
Member

@baiwusanyu-c
I agreed. this is an incorrect usage.
I still think we should give a warning rather than ignore this incorrect usage.

@yyx990803 yyx990803 closed this in 9b7797d Sep 5, 2024
@yyx990803
Copy link
Member

Thanks for catching these cases - the raw ref case does happen when:

  1. The variable name, the ref key, and the ref attribute on the element are all the same.
  2. the SFC is compiled in prod mode.

example

Users won't manually write code like this but it can happen due to the behavior of old template ref usage. So we still need to cover this case.

@linzhe141 linzhe141 deleted the fix-compiler-useTemplateRef branch September 27, 2024 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need test The PR has missing test cases.
Projects
None yet
4 participants