CodexBloom - Programming Q&A Platform

Vue 3: Why is my dynamic component not rendering after state change with v-if?

πŸ‘€ Views: 492 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-16
vue3 composition-api reactivity vue

I'm following best practices but I'm trying to configure I'm building a feature where Hey everyone, I'm running into an issue that's driving me crazy..... I tried several approaches but none seem to work. I'm working on a project and hit a roadblock. I'm working with an scenario where my dynamic component isn't re-rendering when the state changes. I have a parent component that maintains a list of items, and based on user interaction, I toggle the visibility of a child component that displays details for a specific item. I'm using Vue 3 with the Composition API, and I expect that when I change the active item, the corresponding details should display, but it seems like the component is not updating as expected. Here's a simplified version of my code: ```vue <template> <div> <ul> <li v-for="item in items" :key="item.id"> <button @click="setActiveItem(item)">{{ item.name }}</button> </li> </ul> <component v-if="activeItem" :is="activeComponent" :item="activeItem"/> </div> </template> <script> import { ref } from 'vue'; import ItemDetails from './ItemDetails.vue'; export default { components: { ItemDetails }, setup() { const items = ref([{ id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }]); const activeItem = ref(null); const activeComponent = ref('ItemDetails'); function setActiveItem(item) { activeItem.value = item; } return { items, activeItem, activeComponent, setActiveItem }; } }; </script> ``` I tried using `v-if` on the `<component>` tag to conditionally render it based on `activeItem`, but after setting a new active item, the component doesn't seem to update its content. I even checked the console for any reactivity issues, but there are no errors. One thing I noticed is that the component is rendered only once, and subsequent changes to `activeItem` do not affect it. I’m unsure if I am missing something with the reactivity system or if there’s a question with how I am setting the component. Any insights would be greatly appreciated! This is part of a larger API I'm building. How would you solve this? Has anyone else encountered this? My development environment is Windows. Any ideas what could be causing this? I'm working in a Ubuntu 20.04 environment. Any ideas what could be causing this? The project is a CLI tool built with Vue. I'm developing on Ubuntu 20.04 with Vue. I'd be grateful for any help.