fix(useTemplateRef): handle useTemplateRef edge case with vFor
close #12731
the underlying problem is
const elements1 = useTemplateRef('testref')
// instance.refs['testref'] is a shallowRef
instance.refs['testref'].value = [1] // value is an array
const elements2 = ref()
// instance.refs['elements2'] is a ref
instance.refs['elements2'].value = [1] // value is a reactive array
Summary by CodeRabbit
-
Bug Fixes
- Improved handling of template refs in lists to ensure refs collected with v-for are now reactive arrays, providing more accurate and responsive behavior in dynamic list scenarios.
-
Tests
- Added a new test case to verify that refs collected with v-for are properly managed as reactive arrays.
Size Report
Bundles
| File | Size | Gzip | Brotli |
|---|---|---|---|
| runtime-dom.global.prod.js | 100 kB (+4 B) | 38 kB (+7 B) | 34.2 kB (-7 B) |
| vue.global.prod.js | 158 kB (+4 B) | 57.8 kB (+3 B) | 51.4 kB (-5 B) |
Usages
| Name | Size | Gzip | Brotli |
|---|---|---|---|
| createApp (CAPI only) | 46.4 kB (+32 B) | 18.2 kB (+5 B) | 16.6 kB (-16 B) |
| createApp | 54.4 kB (+32 B) | 21.2 kB (+12 B) | 19.3 kB (+13 B) |
| createSSRApp | 58.6 kB (+32 B) | 22.9 kB (+4 B) | 20.9 kB (+9 B) |
| defineCustomElement | 59.2 kB (+32 B) | 22.8 kB (+8 B) | 20.7 kB (+8 B) |
| overall | 68.4 kB (+32 B) | 26.3 kB (+9 B) | 24 kB (-25 B) |
@vue/compiler-core
npm i https://pkg.pr.new/@vue/compiler-core@12733
@vue/compiler-sfc
npm i https://pkg.pr.new/@vue/compiler-sfc@12733
@vue/compiler-dom
npm i https://pkg.pr.new/@vue/compiler-dom@12733
@vue/compiler-ssr
npm i https://pkg.pr.new/@vue/compiler-ssr@12733
@vue/runtime-core
npm i https://pkg.pr.new/@vue/runtime-core@12733
@vue/runtime-dom
npm i https://pkg.pr.new/@vue/runtime-dom@12733
@vue/reactivity
npm i https://pkg.pr.new/@vue/reactivity@12733
@vue/server-renderer
npm i https://pkg.pr.new/@vue/server-renderer@12733
@vue/shared
npm i https://pkg.pr.new/@vue/shared@12733
vue
npm i https://pkg.pr.new/vue@12733
@vue/compat
npm i https://pkg.pr.new/@vue/compat@12733
commit: 2336e2a
I'm not sure whether it would be considered a problem, but this change would also impact $refs.
I'm not sure whether it would be considered a problem, but this change would also impact
$refs.
Thanks for your review. Perhaps this cannot be regarded as a problem, because this kind of behavior already exists in PROD.
- open Playground - main branch
- click the button twice, see console.log
i.refs['elements2']is a Proxy array in PROD
I would be nervous about drawing any conclusions based on that example. getCurrentInstance() is undocumented, so to hit that edge case using documented APIs you'd need to mix <script setup> and the Options API. Not only is it inconsistent between DEV and PROD, it also behaves differently depending on whether you use ref() or ref([]):
It strikes me as an unintentional edge case, rather than something we should be using as precedent.
I'm not entirely clear why $refs isn't reactive, but personally I think it's a decision that should be revisited. That might also allow the implementation of useTemplateRef to be simplified too.
But, given $refs currently isn't reactive, I'd be hesitant to introduce partial reactivity in just the v-for case unless we're planning to move towards full reactivity in future.
Walkthrough
A new test verifies that template refs collected via v-for are reactive arrays. The implementation is updated so that string refs assigned to arrays are now wrapped with shallowReactive, ensuring reactivity. No changes were made to exported APIs or public interfaces.
Changes
| Files | Change Summary |
|---|---|
| packages/runtime-core/tests/helpers/useTemplateRef.spec.ts | Added test to verify that refs collected in v-for are reactive arrays. |
| packages/runtime-core/src/rendererTemplateRef.ts | Updated setRef to wrap array refs with shallowReactive for string refs in template refs. |
Sequence Diagram(s)
sequenceDiagram
participant Component
participant Renderer
participant Reactivity
Component->>Renderer: Render elements with v-for and template ref
Renderer->>Renderer: Collect refs for each element
Renderer->>Reactivity: Wrap refs array with shallowReactive
Reactivity-->>Renderer: Return reactive array
Renderer->>Component: Assign reactive array to template ref
Assessment against linked issues
| Objective | Addressed | Explanation |
|---|---|---|
| Ensure useTemplateRef returns Ref, not ShallowRef, and maintains reactivity (#12731) | ✅ | |
| Ensure collected refs in v-for are reactive arrays matching documentation (#12731) | ✅ |
Possibly related issues
- vuejs/core#12489: The changes make refs collected in a
v-forreactive arrays usingshallowReactive, directly addressing the issue described.
Poem
A bunny hopped through code so neat,
Making refs in loops reactive and sweet.
With shallowReactive arrays,
Now refs update in all ways—
A hop, a test, and bugs in retreat!
🐇✨
✨ Finishing Touches
- [ ] 📝 Generate Docstrings
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Explain this complex logic.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai explain this code block.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and explain its main purpose.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.