Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Vue 3 Composition API ref primitive property not reactive

So while building a tab system I'm taking advantage of useSlots and props in order to create an array of titles (these are the titles of every tab that I open in my tab system), then I create a ref property called selectedTitle in order to bind that title to a class and add css to highlight the tab selected by the user:

This is my component:

<template>
  <div>
    <ul
      class="tag-menu flex space-x-2"
      :class="defaultTagMenu ? 'default' : 'historic'"
      role="tablist"
      aria-label="Tabs Menu"
      v-if="tabTitles && tabTitles.length"
    >
      <li
        @click.stop.prevent="selectedTitle = title"
        v-for="title in tabTitles"
        :key="title"
        :title="title"
        role="presentation"
        :class="{ selected: title === selectedTitle }"
      >
        <a href="#" role="tab">
          {{ title }}
        </a>
      </li>
    </ul>
    <slot />
  </div>
</template>

<script>
import { ref, onMounted, computed, useSlots, provide } from "vue";
export default {
  props: {
    defaultTagMenu: {
      type: Boolean,
      default: true,
    },
  },
  setup() {
    const slots = useSlots();
    const tabTitles = computed(() =>
      slots.default()[0].children.map((tab) => tab.props.title)
    );
    const selectedTitle = ref(tabTitles.value[0]);
    provide("selectedTitle", selectedTitle);
    provide("tabTitles", tabTitles);
    onMounted(() => {
      console.log("V3 mounted!");
    });
    return {
      tabTitles,
      selectedTitle,
    };
  },
};
</script>

This is the scenario: Whenever I close a tab (for the sake of this example that is working and not part of the problem) I want to close the tab and then what I expect is for the selectedTitle ref property of my component to be reactive, meaning to change it's value accordingly depending on the tabTitles array. When I inspect the component I see that every time I close a tab the tabTitles array change but not the selectedTitle value, is always the same value not matter what.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try to use reactive instead of ref

import { reactive } from "vue";

const selectedTitle = reactive(tabTitles.value[0]);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!