Slide 1

Slide 1 text

Fabien Taillon, Partner & CTO at Texeï fabien@texei.com Dynamic Lightning Web Components With great power comes great responsibility

Slide 2

Slide 2 text

Fabien Taillon Partner & CTO at Texeï Salesforce MVP - Hall of Fame Paris Developer Group leader French Touch Dreamin team https://www.texei.com/blog

Slide 3

Slide 3 text

Introduction

Slide 4

Slide 4 text

What is a Dynamic Component A way to dynamically instantiate a component that may not be known before runtime @FabienTaillon - https://www.texei.com/blog

Slide 5

Slide 5 text

Why was This Not Possible Before? @FabienTaillon - https://www.texei.com/blog

Slide 6

Slide 6 text

Why was This Not Possible Before? PERFORMANCE @FabienTaillon - https://www.texei.com/blog

Slide 7

Slide 7 text

We have purposely made the decision of not allowing dynamic component creation yet (the equivalent of $A.createComponent) on the Salesforce platform to make sure we had a clean, statically analyzable and predictable behavior on which to build upon in LWC. … In LWC in the platform we don't allow you to shoot yourself in the foot creating small tiny components, and once we do, we will give you a water pistol first :) Diego Ferreiro Val - Ex Principal Architect LWC https://salesforce.stackexchange.com/questions/244847/not-able-to-render-dynamic-lightning-web-component

Slide 8

Slide 8 text

Valid use cases Most use cases can be achieved with existing lwc:if/lwc:elseif, but: • N°1 use case would be for AppExchanges allowing dynamic components defined through Metadata for instance (think App Builder like) • Can be used to defer big components load if not on the main displayed page • Displayed after a button click • Displayed in another tab • … @FabienTaillon - https://www.texei.com/blog

Slide 9

Slide 9 text

How it works now Just add the capability to the parent component to instantiate dynamic components: 59.0 lightning__dynamicComponent @FabienTaillon - https://www.texei.com/blog

Slide 10

Slide 10 text

How It Works Now And then it’s very easy: • Import component in JS • import MyComponent from 'c/myComponent'; • await import('c/myComponent'); • Define a Dynamic component in template with lwc:component and lwc:is • • Assign in JS the component you want to render • this.componentConstructor = MyComponent; @FabienTaillon - https://www.texei.com/blog

Slide 11

Slide 11 text

Pass Attributes Down Easily pass attributes via markup Depending on dynamic components, you may not know which attributes are need. Properties can be passed down via JavaScript object and lwc:spread directive: JS childProps = { city: "San Francisco", state: "CA" }; Markup @FabienTaillon - https://www.texei.com/blog

Slide 12

Slide 12 text

A Deeper Look

Slide 13

Slide 13 text

Before Dynamic Components Declarative use of components in the template markup All components are known at save time and can be statically analyzed @FabienTaillon - https://www.texei.com/blog

Slide 14

Slide 14 text

Before Dynamic Components Only one round trip to get the whole component and sub-components @FabienTaillon - https://www.texei.com/blog c-my-component c-my-component + c-small-component + c-heavy-component

Slide 15

Slide 15 text

With Dynamic Components Dynamic components will be created by JS and may not be known at that time Depends on how dynamic components are created - more on this later @FabienTaillon - https://www.texei.com/blog

Slide 16

Slide 16 text

With Dynamic Components If not done correctly, one additional round trip per dynamic component @FabienTaillon - https://www.texei.com/blog c-my-component c-my-component + c-small-component c-heavy-component c-heavy-component

Slide 17

Slide 17 text

Dynamic Components To do it correctly, you need to understand how it works and different options available @FabienTaillon - https://www.texei.com/blog

Slide 18

Slide 18 text

Static Import - Statically Analyzable ● Import needed components ● Switch the rendered component by changing the lwc:is variable ● Almost the same as using components in markup and lwc:if ● Everything is downloaded with a single server round trip @FabienTaillon - https://www.texei.com/blog

Slide 19

Slide 19 text

Dynamic Import - Statically Analyzable ● Same as previous, but loading is done on demand ○ connectedCallback ○ Function linked to any action (user click…) ● Not part of the initial load so additional server round trip @FabienTaillon - https://www.texei.com/blog

Slide 20

Slide 20 text

Dynamic Import - Not Statically Analyzable ● Same as previous BUT component is not statically analyzable ● No way for Salesforce to improve loading, caching etc ● As much as possible, don’t use this one @FabienTaillon - https://www.texei.com/blog

Slide 21

Slide 21 text

A Good Use Case

Slide 22

Slide 22 text

Loading Heavy Component Async ● One heavy component ● Not visible during first page load ● Would slow down the whole page if part of the markup @FabienTaillon - https://www.texei.com/blog

Slide 23

Slide 23 text

Loading Heavy Component Async @FabienTaillon - https://www.texei.com/blog

Slide 24

Slide 24 text

Recap

Slide 25

Slide 25 text

Recap ● Use it when you understand how it works ● Not for very small components, server round trip will be slower than initial load ● Use it for big components that may slow down initial load and that are not immediately visible (like unclicked tab) @FabienTaillon - https://www.texei.com/blog

Slide 26

Slide 26 text

Resources Documentation https://developer.salesforce.com/docs/platform/lwc/guide/js- dynamic-components.html Blog post https://developer.salesforce.com/blogs/2023/10/dynamic-co mponents-for-lightning-web-components-is-now-ga RFC https://github.com/jmsjtu/lwc-rfcs/blob/jtu/dynamic-compon ent-creation/text/0119-dynamic-component-creation.md StackExchange answer about performance https://salesforce.stackexchange.com/questions/244847/not- able-to-render-dynamic-lightning-web-component @FabienTaillon - https://www.texei.com/blog

Slide 27

Slide 27 text

Attend the Developer Keynote Build the future with the Salesforce Platform Thursday, Sept. 19 | 2:30 PM PT InterContinental, L3, Grand Ballroom ABC sforce.co/devkeynote

Slide 28

Slide 28 text

Thank you

Slide 29

Slide 29 text

Coffee on us. The first 4,000 attendees to provide feedback on this event will receive a $5 Starbucks gi card. *Restrictions apply. See rules at sforce.co/survey-terms Open the Salesforce Events Mobile App. Navigate to My Event. Select My Surveys. Complete four Session Surveys and present the completed Event Survey page at Badge Pickup to redeem on Thursday, September 19.*

Slide 30

Slide 30 text

Q&A