AURA vs Lightning Web Components
Aura and LWC (Lightning web Components) can communicate with each other using Public APIs and Events. LWC can be embedded into Aura, but not vice versa.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
The new platform is a breakthrough in terms of leveraging web standards. But if you’re a developer who’s new to the salesforce world, or if your org is contemplating a shift from Aura to Lightning Web Components, this article aims to get you a little closer to the answers you need for UI development.
The Basics
For those who are not familiar, the Lightning component framework was launched in 2014 by Salesforce to enable large scale client-side application development on the web. It came with its component model and its modular programming model. From an application perspective, the Lightning Component framework is a UI framework that you can use to develop SPAs (Single Page Applications) for mobile and desktop devices. A Salesforce Developer builds lightning components and a Salesforce administrator assembles the lightning components and creates a lightning page.
Why Did Salesforce Launch Lightning Web Components?
When Lightning Components was first launched by Salesforce, web standards were not a thing. Developing large-scale client-side apps came with a lot of challenges. Apps and components written in different frameworks were not interoperable even though they're all based on the JavaScript. The frameworks provided a high level of abstraction, making each one so different and forcing developers to choose one path. This made skills transfers and ramp-ups in organizations quite hard. As a member of the ECMAScript Technical Committee, Salesforce has been the biggest proponent of standardizing the web to improve flexibility. The last 5 years have seen unprecedented developments in web standardization, thanks to W3C and TC39. The list of standards developed include:
- ECMAScript
- Web components
- Custom elements
- Templates and slots
- Shadow DOM
Lightning Web Components is an extension or the practical enforcement of Web Components standard under the Salesforce platform, to essentially perform the same functions as what Lightning Components does. For the developers out there, learning a new programming language takes time, and gets exceedingly frustrating at times. But the worst happens when a framework you’ve just gotten familiar with is now launching something different or upgrading. If you’ve been on the Aura platform and consider that you are at an intermediate level in expertise, you may be wondering whether you should take Lightning Web Component seriously and if you’d like to invest your time in it.
Why Choose LWC Over Aura?
Sales reps aren’t going to find any difference in their daily Lightning Experience. The difference is only visible to the behind the scenes developers. But Salesforce and the community in general, advises that it is better to choose LWC for building SPAs as much as possible.
Major Advantages of LWC:
- Standardized: The most obvious one, LWC uses core Web Components and gives you everything you need to perform well in browsers. It keeps things simple and is built on code that is native to browsers i.e. HTML, JavaScript, and CSS. Moreover, LWC brings modern JavaScript with ES6+ into the framework, greatly benefiting HTML templates and custom elements.
- Easy to learn: Most of the code is written in standard JavaScript and HTML. This means that there is no abstraction layer in between and you don’t need to learn Aura or any other framework to do most of your tasks.
- Performance: Because of the absence of an abstraction layer, LWC is likely to load and run much faster than Aura components. It is also lightweight and memory-efficient as it is built on Web Components.
- Easy to ramp: As there is no additional framework, LWC developers can make transitions much easier.
- Faster debugging: Debugging is a breeze with LWC as you're more likely to find answers to development questions through forums and communities and other common places on the web.
- Versatility: LWC allows you to utilize full encapsulation.
Differences Between Aura & LWC
If you’re an Aura expert, here’s a gist of notable differences you’ll find between coding in Aura and LWC:
- Bundle structure: LWC requires manual creation of a folder to host all your component files. Component HTML, JS, Configuration files are mandatory. CSS and SVG are optional.
- Naming Convention: The only noted difference in naming is while you’re referring component in another component. The new syntax uses kebab-case, instead of the camelCase ( <c-hellowrold> instead of <c:helloworld>) and the component must have a closing tag.
- Events: Unlike component or application events in Lightning Component, LWC used Standard DOM events.
- Lightning Locker: Lightning Locker is enabled by default in all custom LWC.
While this is not an exhaustive list, it gives you a small insight into how much change you might have to get accustomed to.
Co-existence and Interoperability
It has been made clear that as of, Lightning components are not going away. They will continue to exist in parallel. Aura and LWC can communicate with each other using Public APIs and Events. LWC can be embedded into Aura, but not the other way round. While you should always think of choosing LWC over Aura, there are some limitations. LWC doesn’t support everything that Aura can do yet. If you do want to use an unsupported feature, experience, or interface, develop everything in LWC and wrap it up inside an Aura component. However, if you want to use an unavailable base component, you may need to develop everything in Aura.
Composition Strategies for Using Aura and Lightning Web Components Together
Nested Composition
You can use LWC and Aura Components side by side, but Aura is the ultimate parent. An Aura Parent can contain both LWC and Aura components, but an LWC parent can only contain LWC components.
Side-by-side composition
Interaction between Aura components and LWC is possible in many ways if they are nested or contained within a parent:- interacting directly with child component attributes or through application and component events- send and receive information through JavaScript CustomEvent objects- interacting with child component methods and properties exposed through public APIsBoth Aura components and LWC can send and receive data through LDS. As of now, there is no mechanism of communication between Aura and Lightning web components that aren’t in the same hierarchy.
Migrating From Aura to LWC
Your org may be contemplating a migration strategy to move towards LWC. Before you decide on taking the first footsteps towards the new path, understand that the fundamentals of both the models are entirely different and it isn’t simply a line-by-line conversion. However, on the brighter side, it’s an opportunity to simplify, redesign, and reorganize. Take it one step at a time, and once you complete one migration you may be in a better position to understand the need for a larger effort. Choosing migration patterns is much easier for smaller components. For those that have no JavaScript at all, you can simply swap the Aura HTML Syntax with its Lightning web counterpart as you go. Redesign, then rebuild. For larger components, you may need to start from bottom-up and understand how to manipulate deeply nested hierarchies. Of the looming questions regarding LWC, most developers want to see the best of both worlds scenario where they can make the most of VF’s server-side rendering and client-side handling by LWC.
Conclusion
LWC brings with it all the good things about modern JavaScript and Web Components in a lightweight framework. While you may be wondering whether you should switch as a developer, it should be noted that your org should develop new components on LWC.
- Implement deduplication and auto duplicate detectors
- Drive better insights which are not possible by standard reports
- Implement complex record/lead/opportunity assignment rules
- Create extensive automation to better run the process of the Sales lifecycle.
Moreover, since it is mostly developed on HTML, CSS, and JS you may not find it all too difficult to get used to the new syntaxes and conventions. Migrations as of now may be slow, due to limitations in terms of communication between Aura and LWC components but further releases might change the precedent, allowing orgs to completely move over to LWC. But one thing is for sure, LWC is here to stay and is a welcome change as it makes ramp-ups and transitions much simpler, allowing all the good parts of SPAs to scale easily. The robust framework simplifies the need for organizations to look into integrating other frameworks into Aura (which only lead to more chaos and unreadability).
Published at DZone with permission of Tanisha Kumawat. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments