What is Container Component ?
Container Components are the components that are not the direct owner/parent of any other component but contain another component between their tags wherever they are defined. For ex:- If I make two lightning components named:- one.cmp and two.cmp and instead of calling two.cmp inside one.cmp I do something like this:-While learning about component events bubble and capture phase I have told about hierarchy or containment hierarchy. So, let's see what a container component is and how this hierarchy is implemented. I'll be using the previous code implemented till now so if you just joined or want access to the current implementation you can read my previous blogs in the series listed here. Or at least have a look at the bubble phase implementation here.
So, we have 2 components already implemented:- LightningEventsCompContainer and LightningEventsComp1. Now, I am going to add one more component i.e. LightningEventsCompWrapper. This component is basically a container component which will be wrapping our base component i.e. LightningEventsComp1 and will be a direct child of LightningEventsCompContainer. Let's jump on to the code now for a deeper understanding:-
LightningEventsCompContainer.cmp
<!-- Wrapper/Parent component called directly from the Lightning Application --> <aura:component> <!-- Attribute to store total income coming through the event --> <aura:attribute name="totalIncome" type="decimal" default="0" ></aura:attribute> <!-- Handler defined to handle 'totalIncomeComponentEvent' name same as used in registerEvent tag --> <aura:handler name="totalIncomeComponentEvent" event="c:LightningComponentEvent" action="{!c.handleTotalIncomeComponentEvent}"></aura:handler> Outer Component <!-- Inner component section with border --> <div class="innerComponent"> Inner Component Section Begin <!-- Calling the inner component container --> <c:LightningEventsCompWrapper> <!-- Calling the source component --> <c:LightningEventsComp1 /> </c:LightningEventsCompWrapper> Inner Component Section End </div> <!-- Section to show total income --> <span class="totalIncome">Total Income = {!v.totalIncome}</span> </aura:component>
LightningEventsCompWrapper.cmp
<!-- Container to wrap inner component --> <aura:component> <!-- Event handler in wrapper component --> <aura:handler name="totalIncomeComponentEvent" event="c:LightningComponentEvent" action="{!c.handleTotalIncomeComponentEvent}" includeFacets="true"></aura:handler> <!-- Wrapper Component --> <div class="wrapperComponent"> Wrapper Component Section Begin <!-- Calling the body i.e. the data between the tags of wrapper/container component --> {!v.body} Wrapper Component Section End </div> </aura:component>
There is one more thing to notice i.e. I have included an extra attribute named includeFacets="true" in the component event handler defined in wrapper component. This is because generally our wrapper component is only containing the source component in it's tags and therefore it'll not be able to handle the event fired by our source component, as it is not the direct owner/parent. So, to make it able to handle the events, we have to include includeFacets="true" attribute in the handler defined in our wrapper component.
LightningEventsCompWrapperController.js
({ // Function invoked when event is handled handleTotalIncomeComponentEvent : function(component, event, helper) { alert('Event handler at the wrapper component'); } })
LightningEventsCompWrapper.css
.THIS {
}
/* Border of wrapper component */
.THIS.wrapperComponent {
border: 1px solid red;
}
So, we have given the border to our wrapper component using border: 1px solid red; in the wrapperComponent class which is used by our div.I have configured all the handlers to work in bubble phase i.e. without having phase="capture" attribute in them and when you run this in your browser and click on Calculate Total Income button, you get the following outputs:-
![]() |
Event handler at the source component is fired |
![]() |
Event handler at the wrapper component is fired |
![]() |
Event handler at the parent component is fired |
Tired of reading or just scrolled down, don't worry, you can watch the video too.
So, in this post we learned about container components and how they are implemented. The whole source code is available in the componenteventwrapper branch of my github repository that you can access directly from here. These container components play a vital role in order to make components generic and will be used in the future tutorials when we'll learn about application events and we'll differentiate it from the component events. If you liked this post, then subscribe to this blog and share among others in your network so that they can get benefit too. Also, make sure to give your feedback in comments.
Hi Rahul,
ReplyDeletem using the lightning : verticalNavigationItemIcon inside the lightning : verticalNavigation and passing the attribute selectedItem="" .so onClick of particular icon. particular page got displayed which is linked to the particular icon as well as icon got selected with dark grey shade on it .so that it can show which icon is got clicked and i m also using the next prev button to show the page linked with the icon. but the issue is, icon selection is not changing on click of button. I hope you understand the issue.please help me.
Hi Priya,
DeleteAs far as I know about lightning:verticalNavigation, it cannot be customized upto a higher level as I am unable to find functionality for overriding the effects. So, I suggest you to use lightning vertical navigation design from slds itself:- https://www.lightningdesignsystem.com/components/vertical-navigation/ and implement any functionality that you want by applying your own custom javascript.
Your tutorials are really good and helpful
ReplyDeleteHappy to see that you liked it :-) Make sure to share it in your network too..!!
Delete