4

Let's say I have @track dataSource[] which contains objects {id,price} Why if I for example change the value

this.dataSource[1].price = 1234;

i can't get changes in markup? Markup

<lightning-datatable
            data={dataSource} 
            columns={columns} 
            key-field="lotId">
   </lightning-datatable>

1 Answer 1

9

Because @track works differently for different Types. For Objects, it tracks changes to properties. Arrays are treated differently, you need actually re-assign the property, so that UI is updated.

this.dataSource[1].price = 1234;
this.dataSource = [...this.dataSource];

In the following example with Object UI would be updated.

@track user = {name: 'John', age: 25};

ageUser() {
   this.user.age += 1;
}
1
  • 4
    Note, as of Spring '20, @track is no longer required to make arrays or objects reactive when reassigned. In the examples above, this.dataSource would not need to be decorated, but the user object would still need to be decorated with @track.
    – pchittum
    Feb 19, 2020 at 9:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .