@track in Lightning Web Components
Did you know that you can pass complex types of data from Lightning Web Components to Apex controllers?
I've recently implemented a Lightning data table with pagination and sorting so I've created a data structure "queryParameters" like this (*):
@track queryParameters = {
sortFieldName: '', sortDirection: '', pageNumber: 1, pageSize: 8
};
Then I've referenced the queryParameters variable in a @wire call to an Apex controller as below:
@wire( getAvailableRecords, { queryParameters: '$queryParameters' } )
Then found a couple of interesting details:
Recommended by LinkedIn
So I've ended up coding the changes to queryParameters like below, to cause @track to not ignore changes:
// refresh data when page is changed
this.queryParameters = {
...this.queryParameters
, pageNumber: pageNumber
};
...
// refresh data when sorting criteria is changed
this.queryParameters = {
...this.queryParameters
, sortFieldName: fieldName
, sortDirection: sortDirection
}
(*) @track is no longer needed since Spring '20 release and all fields in a LWC class are reactive. @track was mentioned to make the point clear.
It is interesting though that the observed behavior is exactly opposite of what is described in Spring '20 and has been reported as a bug.
Senior Salesforce Developer ( Salesforce is my Passion - I Live & Dream in Salesforce )
2yThe other way is - pass json string from lwc and deserialize it on Apex side 🙂
Salesforce Admin + Developer + Release Manager | Salesforce Professional | Trailhead Mentor
2yHi Fernando F. can you share the Article please, I want to go through it..