Thursday, 28 March 2019

Lightning Web Components Best practices


We did one live session on Lightning Web Components (LWC) best practices with "René Winkelmeyer" in ApexHours. In that session we talk about how to build rich, efficient, and resilient Lightning Web Components. That webinar covered best practices around
  1. Using public, and private properties for effective component composition.
    1. When we should use @api or @track variable
  2. Event communication for child-to-parent as well as for sibling components (pubsub).
    1. Parent to Child Communication
    2. Child to Parent Communication
    3.  pubsub
  3. When, and when not, to use Apex with Lightning Web Components
    1. UI Record Api in LWC
    2. Lightning Data Service in LWC (Lightning record form)
  4. Aura interoperabilitys Recipes, Patterns and Best Practices
  5. How to debug Lightning Web Components
  6. How to test Lightning Web Component (Lightning Testing Service)
    1. Jest Tests for Lightning Web Components ( LTS )
       
Here is some best practice :-

1) LWC component Bundle naming convention 

 Check this post to learn about what is camelCase, PascalCase and kebab-case.
  1. Html file : Use camel case to name your component and use kebab-case to reference a component in the markup
  2. JavaScript File : Java Script Class name should be in PascalCase
  3. Bundle Component : use camelCase.  

2) Apex

There are two way to call Apex class in Lightning web component.
  1. Imperatively
  2. Wire 
    1. Wire a property
    2. Wire a function  

Wire Vs Imperatively :

As per Lightning component best practices use @wire over imperative method invocation. @wire fits nicely in the overall Lightning Web Component reactive architecture. Salesforce is building some performance enhancement features that are only available with @wire. But there are a few use cases,, that require you to use imperative Apex.

Wire Property Vs Wire Function :

Prefer wiring to a property. This best practice applies to @wire in general (not just to wiring Apex methods).

3) Lightning Data Service

As per LWC best practice use Lightning Data Service functions to create,Record, and delete a record over invoking Apex methods. Yes there are some use cases where you need to multiple records then we can use Apex methods.

Lightning Data Service is built on top of User Interface API. UI API is a public Salesforce API that Salesforce uses to build Lightning Experience. Like its name suggests, UI API is designed to make it easy to build Salesforce UI. UI API gives you data and metadata in a single response

Give preference to user interface form-type in below order.
  1. lightning-record-form : It is the fastest/most productive way to build a form.
  2. lightning-record-view-form : If you need more control over the layout, want to handle events on individual input fields, or need to execute pre-submission
  3. @wire(getRecord) : If you need even more control over the UI, or if you need to access data without a UI

4) Event in LWC


There are typically 3 approaches for communication between the components using events.
  1. Communication using Method in LWC ( Parent to Child )
  2. Custom Event Communication in Lightning Web Component (Child to Parent )
  3. Publish Subscriber model in Lightning Web Component ( Two components which doesn't have a direct relation )

5) Streaming API, Platform Event, Change Data Capture


The lightning/empApi module provides access to methods for subscribing to a streaming channel and listening to event messages. All streaming channels are supported, including channels for platform events, PushTopic events, generic events, and Change Data Capture events. This component requires API version 44.0 or later. The lightning/empApi module uses a shared CometD connection. Example Code.

6) How to debug LWC

Use Chrome pretty JS setting to see unminified JavaScript and Debug Proxy Values for Data. Here is example.
  • Enable Debug mode
    • It gives unminified Javascript
    • Console warnings
    • Pretty data structure
  • Caution - it reduces performance of Salesforce, make sure its disabled in production

7) Use Storable Action

Use Storable Action, It will reduces call to Server. Syntax - @AuraEnabled(cacheable=true)

Caution: A storable action might result in no call to the server. Never mark as storable an action that updates or deletes data.
For storable actions in the cache, the framework returns the cached response immediately and also refreshes the data if it’s stale. Therefore, storable actions might have their callbacks invoked more than once: first with cached data, then with updated data from the server.




Some more key point
  1. Use Playground to markup Custom Components
  2. Use Local Development 
  3. Refer Lightning Web Component Recipe for best practices

Recording

Here is recording of session.



If you missed our first session on Introduction to Lightning Web component then please check our this recording.



Further Learning :

1) Introducing Lightning Web Components Recipes, Patterns and Best Practices


Please check below post on Lightning Web Components:-
  1. Lightning Web Components ( LWC ) in Salesforce with Non-Scratch Org
  2. Design attributes in Lightning Web Components | CSS and SVG Files | Lightning Web Components | targetConfigs
  3. Toast Notification in Lightning Web Components | ShowToastEvent |  (LWC)




Thanks,
Amit Chaudhary

No comments:

Post a comment