[Dreamforce 24] Dynamic LWC: With great power comes great responsibility
Dynamic LWC is a long awaited feature. No need to hardcode LWC names, create them dynamically on the fly. It's great but can have a severe impact on performance if used incorrectly. Let’s deep dive!
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
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
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 • <lwc:component lwc:is={componentConstructor}></lwc:component> • Assign in JS the component you want to render • this.componentConstructor = MyComponent; @FabienTaillon - https://www.texei.com/blog
lwc:is={componentConstructor} text="I love dynamic components!"></lwc:component> </template> 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 <template> <lwc:component lwc:is={componentConstructor} lwc:spread={childProps}></lwc:component> </template> @FabienTaillon - https://www.texei.com/blog
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
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
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
• 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
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.*