Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

So I have this problem in vue 3 where I remove one item from a list and it rerenders the…

  1. User

    So I have this problem in vue 3 where I remove one item from a list and it rerenders the list I and I use any data on the list as the items are back to default, I'm using Vue 3 composition api ,script setup> syntax

  2. Assistant

    It sounds like you have an issue with the reactivity of your list in Vue 3 when removing an item. To help you solve this issue, I'll provide a step-by-step example using the Vue 3 Composition API and the script setup syntax. Please adapt the example to your specific use case.

    1. First, create a new Vue 3 project using the Vue CLI, or start a new component in your existing project.

    2. In your component, define the template section to render a list of items and a button to remove an item by its index:

    <template>
      <div>
        <ul>
          <li v-for="(item, index) in items" :key="item.id">
            {{ item.name }} - <button @click="removeItem(index)">Remove</button>
          </li>
        </ul>
      </div>
    </template>
    
    1. In the same component file, start the script setup section, and import the ref and reactive utilities from Vue:
    <script setup>
    import { ref, reactive } from "vue";
    
    1. Define your list of items as a reactive object, and a function to remove an item by its index:
    const items = reactive([
      { id: 1, name: "Item 1" },
      { id: 2, name: "Item 2" },
      { id: 3, name: "Item 3" },
    ]);
    
    const removeItem = (index) => {
      items.splice(index, 1);
    };
    

    The complete component would look like this:

    <template>
      <div>
        <ul>
          <li v-for="(item, index) in items" :key="item.id">
            {{ item.name }} - <button @click="removeItem(index)">Remove</button>
          </li>
        </ul>
      </div>
    </template>
    
    <script setup>
    import { reactive } from "vue";
    
    const items = reactive([
      { id: 1, name: "Item 1" },
      { id: 2, name: "Item 2" },
      { id: 3, name: "Item 3" },
    ]);
    
    const removeItem = (index) => {
      items.splice(index, 1);
    };
    </script>
    

    With this setup, your list should update correctly when removing items. The key here is to use the Vue reactive() function to create a reactive object for the list, and remember to uniquely identify your list items using the :key attribute.

  3. User

    Can we simplify and improve this code: import { reactive, ref, computed } from "vue"; import Countries from "./CountriesSwitch.vue";

    const uniqueUUid = () => parseInt(crypto.getRandomValues(new Uint32Array(1))[0] );/* + "000" */ const getInitialItems = () => [uniqueUUid()];

    const items = ref(getInitialItems()); const selected = reactive({ list: [] });

    const addSlot = () => { const newItem = { uuid: uniqueUUid(), select: { country: "" } }; items.value.push(newItem.uuid); };

    const removeData = (id) => { const index = items.value.findIndex((item) => item === id); if (index !== -1) { items.value.splice(index, 1);

    const selectedIndex = selected.list.findIndex((item) => item.uuid === id);
    if (selectedIndex !== -1) {
      selected.list.splice(selectedIndex, 1);
    }
    

    } };

    const getSelected = (n) => { const index = selected.list.findIndex((x) => x.uuid === n.uuid); if (index === -1) { selected.list.push(n); } else { selected.list[index] = n; } return selected.list; };

  4. Assistant

    The code you provided can be improved by encapsulating the functionalities in separate composable functions and making the code more modular. Here's an optimized and more readable version of your code:

    import { reactive, ref, computed } from "vue";
    import Countries from "./CountriesSwitch.vue";
    
    const generateUUID = () => parseInt(crypto.getRandomValues(new Uint32Array(1))[0]);
    
    const useItems = () => {
      const items = ref([generateUUID()]);
    
      const addSlot = () => {
        const newItem = { uuid: generateUUID(), select: { country: "" } };
        items.value.push(newItem.uuid);
      };
    
      const removeItem = (id) => {
        const index = items.value.findIndex((item) => item === id);
        if (index !== -1) items.value.splice(index, 1);
      };
    
      return { items, addSlot, removeItem };
    };
    
    const useSelected = () => {
      const selected = reactive({ list: [] });
    
      const insertOrUpdateSelected = (n) => {
        const index = selected.list.findIndex((x) => x.uuid === n.uuid);
        if (index === -1) {
          selected.list.push(n);
        } else {
          selected.list[index] = n;
        }
      };
    
      const removeSelected = (id) => {
        const selectedIndex = selected.list.findIndex((item) => item.uuid === id);
        if (selectedIndex !== -1) selected.list.splice(selectedIndex, 1);
      };
    
      return { selected, insertOrUpdateSelected, removeSelected };
    };
    
    // Usage inside the setup() function of your component
    const { items, addSlot, removeItem } = useItems();
    const { selected, insertOrUpdateSelected, removeSelected } = useSelected();
    
    const removeData = (id) => {
      removeItem(id);
      removeSelected(id);
    };
    
    const getSelected = (n) => {
      insertOrUpdateSelected(n);
      return selected.list;
    };
    

    Here, I created two composable functions useItems and useSelected that encapsulate the functionalities related to items and selected respectively. This makes the code more readable and easier to reason about. Also, I renamed some of the utility functions and consts to better convey their purpose.