{"id":668,"date":"2022-07-06T21:28:00","date_gmt":"2022-07-07T02:28:00","guid":{"rendered":"https:\/\/blog.felineflock.com\/?p=668"},"modified":"2024-01-03T21:40:29","modified_gmt":"2024-01-04T03:40:29","slug":"how-to-use-track-with-complex-types-in-lightning-web-components","status":"publish","type":"post","link":"https:\/\/blog.felineflock.com\/index.php\/2022\/07\/06\/how-to-use-track-with-complex-types-in-lightning-web-components\/","title":{"rendered":"How to Use @track with Complex Types in Lightning Web Components"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"213\" src=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-35-1024x213.png\" alt=\"\" class=\"wp-image-669\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-35-1024x213.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-35-300x63.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-35-768x160.png 768w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2024\/01\/image-35.png 1478w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p id=\"ember2684\">Did you know that you can pass complex types of data from Lightning Web Components to Apex controllers?<\/p>\n\n\n\n<p id=\"ember2685\">I&#8217;ve recently implemented a <a href=\"https:\/\/developer.salesforce.com\/docs\/component-library\/bundle\/lightning-datatable\/documentation\">Lightning data table<\/a> with pagination and sorting so I&#8217;ve created a data structure &#8220;queryParameters&#8221; like this (*):<\/p>\n\n\n\n<p id=\"ember2685\"><code>@track queryParameters = { <\/code><\/p>\n\n\n\n<p id=\"ember2685\">     <code>sortFieldName: '', <\/code><\/p>\n\n\n\n<p id=\"ember2685\">     <code>sortDirection: '', <\/code><\/p>\n\n\n\n<p id=\"ember2685\">     <code>pageNumber: 1, <\/code><\/p>\n\n\n\n<p id=\"ember2685\">     <code>pageSize: 8 <\/code><\/p>\n\n\n\n<p id=\"ember2685\"><code>};<\/code><\/p>\n\n\n\n<p id=\"ember2686\">Then I&#8217;ve referenced the queryParameters variable in a @wire call to an Apex controller as below: <\/p>\n\n\n\n<p id=\"ember2686\"><code>@wire( getAvailableRecords, { queryParameters: '$queryParameters' } )<\/code><\/p>\n\n\n\n<p id=\"ember2687\">Then found a couple of interesting details:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The Apex method will require a <strong>Map&lt;Object, Object&gt; or Map&lt;Object, String&gt;<\/strong> for complex\/composite Javascript types such as &#8220;queryParameters&#8221;. The parameter will not be received if you declare it as a class with @AuraEnabled members. You will have to receive it as a Map instead.<\/li>\n\n\n\n<li>If you change a member of the object queryParameters, even though you&#8217;ve annotated it with @track (*), it won&#8217;t track and the component will not rerender. You will have to modify the queryParameters object entirely &#8211; instead of just one of its members &#8211; to cause the expected reaction (that is, the @wire method should be called when queryParameters changes).<\/li>\n<\/ol>\n\n\n\n<p id=\"ember2689\">So I&#8217;ve ended up coding the changes to queryParameters like below, to cause @track to not ignore changes: <\/p>\n\n\n\n<p id=\"ember2689\"><merlin-component id=\"merlin-code-summarizer\" class=\"merlin-code-summarizer\"><\/merlin-component><code><merlin-component id=\"merlin-code-summarizer\" class=\"merlin-code-summarizer\"><\/merlin-component>\/\/ refresh data when page is changed <\/code><\/p>\n\n\n\n<p id=\"ember2689\"><merlin-component id=\"merlin-code-summarizer\" class=\"merlin-code-summarizer\"><\/merlin-component><code>this.queryParameters = { <\/code><\/p>\n\n\n\n<p id=\"ember2689\"><merlin-component id=\"merlin-code-summarizer\" class=\"merlin-code-summarizer\"><\/merlin-component><code>     ...this.queryParameters, <\/code><\/p>\n\n\n\n<p id=\"ember2689\"><merlin-component id=\"merlin-code-summarizer\" class=\"merlin-code-summarizer\"><\/merlin-component><code>     pageNumber: pageNumber <\/code><\/p>\n\n\n\n<p id=\"ember2689\"><code>};<\/code><\/p>\n\n\n\n<p id=\"ember2689\"><code>... <\/code><\/p>\n\n\n\n<p id=\"ember2689\"><code>\/\/ refresh data when sorting criteria is changed<\/code><\/p>\n\n\n\n<p id=\"ember2689\"><code>this.queryParameters = { <\/code><\/p>\n\n\n\n<p id=\"ember2689\"><code>     ...this.queryParameters, <\/code><\/p>\n\n\n\n<p id=\"ember2689\"><code>     sortFieldName: fieldName, <\/code><\/p>\n\n\n\n<p id=\"ember2689\"><code>     sortDirection: sortDirection <\/code><\/p>\n\n\n\n<p id=\"ember2689\"><code>}<\/code><\/p>\n\n\n\n<p id=\"ember2690\">(*) <a href=\"https:\/\/help.salesforce.com\/s\/articleView?id=release-notes.rn_lwc_track.htm&amp;type=5&amp;release=224\">@track is no longer needed since Spring &#8217;20 release<\/a> and all fields in a LWC class are reactive. @track was mentioned to make the point clear.<\/p>\n\n\n\n<p id=\"ember2691\">It is interesting though that the observed behavior is exactly opposite of what is described in Spring &#8217;20 and <a href=\"https:\/\/github.com\/salesforce\/lwc\/issues\/2249\">has been reported as a bug<\/a>.<\/p>\n\n\n\n<p id=\"ember2692\">Thanks to <a href=\"https:\/\/www.linkedin.com\/in\/kkteja\/\">Krishna Teja<\/a> for <a href=\"https:\/\/blog.salesforcecasts.com\/pass-complex-data-types-from-lightning-web-component-to-apex-methods\/\">this article that gave more clarity on passing complex data types from LWC to Apex<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did you know that you can pass complex types of data from Lightning Web Components to Apex controllers? I&#8217;ve recently implemented a Lightning data table with pagination and sorting so I&#8217;ve created a data structure &#8220;queryParameters&#8221; like this (*): @track queryParameters = { sortFieldName: &#8221;, sortDirection: &#8221;, pageNumber: 1, pageSize: 8 }; Then I&#8217;ve referenced [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-668","post","type-post","status-publish","format-standard","hentry","category-blog"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/comments?post=668"}],"version-history":[{"count":1,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":670,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/668\/revisions\/670"}],"wp:attachment":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/media?parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/categories?post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/tags?post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}