What is the correct way of updating an object in an array?

Viewed 112

I have an array of objects, in my code I list out these

{#each advances as tech}
    <Advance tech={tech} addToCart={addToCart}/>
{/each}

The add to cart function updates a variable on the object:

  const addToCart = (tech) =>  {
      tech.Cart = true;
  }

This does not trigger reactivity, how should I do this?

2 Answers

Assuming you want to do more than simply toggling a flag you can also do:

const addToCart = (tech) =>  {
  tech.Cart = true;
  advances = advances;
}

This will force a reactivity trigger on the array.

Related