Home  >  Q&A  >  body text

Loss of mutable responsiveness in Composition API

I'm trying to get the ranks array of ranks_options from the useForm helper but it can't keep track of the current array

Please check below is my code

const form = useForm({
    name: null,
    ranks: [],
});

function addRanks() {
    form.ranks.push({ name: null, id:null});
}

ranks_options = form.ranks.map(function (value) {
    return value.name;
});

P粉141925181P粉141925181399 days ago515

reply all(1)I'll reply

  • P粉523335026

    P粉5233350262023-09-17 19:32:36

    Convert it to a computed property :

    const ranks_options = computed(() => form.ranks.map(value => value.name)
    

    This will set form.rank as a dependency and recalculate its value when the dependency changes.

    reply
    0
  • Cancelreply