fix(Accordion/Tabs): use item value as stable key to avoid remounts#6380
Conversation
Both `UTabs` and `UAccordion` used `:key="index"` when rendering their `v-for` items. When items are added, removed, or reordered, Vue sees "the same key at the same position," reuses the DOM element, and swaps its inner content — which unmounts any stateful descendant of the reused slot. For `UTabs` this means every tab after the changed position loses local state (counters, form inputs, subscriptions). For `UAccordion` the same pattern causes content remounts and occasional hydration oddness when combined with lazy-loaded islands. Switch both to `:key="get(item, valueKey) ?? index"` — the same expression already used for `:value` — so items with a user-provided `value` get a stable identity across reorders, while items without a value keep the previous index-based behaviour. Closes nuxt#5841
📝 WalkthroughWalkthroughAccordion and Tabs components were updated to use each item’s configured value as the Vue Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
Clarify in the `TabsItem.value` and `AccordionItem.value` JSDoc that the value doubles as the Vue `key` for rendered items — consumers should provide a stable value if they add, remove, or reorder items and want per-item state preserved across those changes.
🔗 Linked Issue
Closes #5841
📚 Description
Both
UTabsandUAccordioncurrently key theirv-foritems by the iteration index:When items are added, removed, or reordered, Vue sees "the same key at the same position," reuses the DOM element, and swaps its inner content — which unmounts any stateful descendant of the reused slot. For
UTabsthis means every tab after the changed position loses local state (counters, form inputs, subscriptions). ForUAccordionthe same pattern causes content remounts and — combined with lazy-loaded islands — occasional hydration oddness.Switch both to
:key="get(item, valueKey) ?? index"— the same expression already used for:value— so items with a user-providedvalueget a stable identity across reorders, while items without a value keep the previous index-based behaviour (no regression for existing users who don't providevalue).🔖 Checklist
pnpm install.pnpm lint --fix.pnpm docs:build.pnpm test.