The independent daily for Salesforce admins, developers and architects

Curated by Abhinav (opens in a new tab)

Five ways Lightning Web Components can share data between components (opens in a new tab)

You will see how a parent passes data to a child, how a child reports back, and when to use Lightning Message Service or Lightning Data Service instead.

Why read this

The piece also lists common mistakes to avoid.

Parent to child

  • @api public property
  • Parent sets HTML attribute
  • Child reads the value

Child to parent

  • Child dispatches CustomEvent
  • Detail carries the payload
  • Parent handles the event
Parent-to-child uses public properties; child-to-parent uses custom events

The article surveys five ways Lightning Web Components can communicate. The simplest is parent-to-child communication through a public property marked with the @api decorator, where the parent sets an HTML attribute and the child receives it. Code examples show a child component declaring a message property and a parent passing a welcome string into it.

When a child needs to notify its parent, it dispatches a custom event carrying a detail payload. The example has a child button fire a valuechange event, and the parent handles it and stores the selected value. The author notes that this lets the child report upward without reaching into the parent’s internal properties.

For components with no parent-child relationship, such as a customer information panel and a related sales panel, the article describes Lightning Message Service. The steps are to create a message channel, import it into a publisher, publish a message, subscribe in the receiving component and process the payload. Lightning Data Service is presented for cases where the data is a Salesforce record, using getRecord through a wire adapter, with the benefit of caching and record sharing across components. Shared JavaScript modules are mentioned for reusable logic.

The article closes with a table matching each method to its best use and a list of mistakes to avoid, such as using custom events where a public property is enough, creating unnecessary message channels, and calling Apex when Lightning Data Service already covers the need.

Read the original at dev.to (opens in a new tab)

Worth reading?

Must-read

Seven bugs that passed a 113-test Salesforce suite and the fix for each (opens in a new tab)

You will learn seven ways Apex and LWC code can pass its tests yet fail in a real org, including keyset paging, retried saves, restricted picklists and cacheable methods.

Why read this

Each comes with a concrete fix and a check you can run on your own code.

Recursion guard (bug 2)

  • Static Id set skips retried records
  • Silent skip with all-or-none off
  • Compare old and new values instead

Cacheable refresh (bug 5)

  • cacheable=true returns stale rows
  • Mocked Jest tests cannot catch it
  • Drop cacheable or use refreshApex()
The author's fixes for two of the seven bugs, set side by side

The author’s starter kit had 113 passing tests, 68 Apex and 45 Jest, and deployed cleanly to a real org. A tester clicking through the data table and a reviewer reading the code together found seven bugs that no test had caught. The gaps lay between what the tests checked and what a live org did.

The first bug was a paged table that kept showing page one. The server used a keyset cursor, and a cached browser component sent only a page number, so the server treated every request as page one. The tests called each version with the arguments it expected, so an old client against a new server was never tested. The fix is to fall back to offset paging when a later page arrives without a cursor, and to check what an old client would receive after changing an @AuraEnabled contract. The second bug was a static Id-based recursion guard that silently skipped retried records when a save ran with all-or-none off. The fix is to compare old and new values and recalculate for work that is safe to repeat, keeping an Id guard only for work that must happen exactly once.

The third bug was a logger whose Platform Event carried any LoggingLevel name, while the Log__c picklist allowed only ERROR, WARN, INFO and DEBUG. Logs finer than DEBUG failed silently in the subscriber trigger. The fix is to store anything finer as DEBUG, and to test every restricted picklist with a value outside its list. The fourth was an example Account trigger shipped switched on, which changed the data of anyone who deployed the kit. Example code should ship disabled, and the tests should enable handlers only while they run.

The fifth bug was a refresh() method that could show stale rows, because the backing Apex method was cacheable. Jest mocks cannot catch this, so it appears only in a real browser. The fix is to drop cacheable=true where current data matters, or to use @wire with refreshApex(). The sixth was a no-CLI zip, built by hand, that still held six older files. The fix is to build the zip from the source folder in one script, compare every file, and deploy the zip as the final test. The seventh was an offset calculation that overflowed for very large page numbers before the row-limit check ran. The fix is to compare before multiplying.

The author groups the bugs into gaps between versions, between code and platform, between the developer’s org and the buyer’s, and at input edges nobody types on purpose. The closing advice is that passing tests cover only what the developer thought of, so manual testing and fresh review are needed before calling code production-ready.

Read the original at dev.to (opens in a new tab)

Worth reading?