Lifecycle Hooks in LWC: A Detailed Guide
S2 Labs by Shrey Sharma | Salesforce Training Institute
Only Salesforce-focused Training Institute. ??
Building web components in Salesforce is challenging in a web development journey. While Lightning Web Components is a newer technology, people need to know how lifecycle hooks work in LWC. The lifecycle hooks are essential to the development to ensure the high quality and smooth running of applications.
They give the developers powers to trigger the components during various stages of their lifecycle. This allows a more efficient approach to building web components in the Salesforce Ecosystem.
So, in this blog, we will explore the lifecycle hooks in LWC and how they are used to fine-tune the development process. This will help you understand and implement the best practices in your development journey.
But first, let’s clarify the lifecycle hooks and how they work.
What are Lifecycle Hooks in Lightning Web Components?
You must have an understanding of the life cycle of humans. Let’s use it as an analogy to understand the lifecycle hooks in LWC. When we are born, we are known as infants; that is the first stage of our life as humans. Soon, the infant becomes a teenager, followed by an adult, and at last, an elderly person.
After this, the end arrives and finishes the cycle of life. The life cycle is similar to the life cycle of humans as they begin at the time of creation and end when they are removed or destroyed.
The stages of this lifecycle of a component on a page are called hooks, hence lifecycle hooks. With the help of these hooks, you may regulate, enhance, and react to particular occurrences and changes in your component’s lifecycle. Robust and responsive component creation requires understanding them and their practical application.
Types of Lifecycle Hooks in LWC
A lifecycle hook is a callback method triggered at a specific phase of a component instance’s lifecycle. Now, there are six types of hooks throughout the lifecycle of a component that can be applied to it. Let’s take a look at these below.
Constructor
The constructor hook can be similar to the birth stage in the human lifecycle. This hook flows from parent to child, meaning it fires in the parent first. The child elements do not exist at this stage; hence, you cannot access them.
As there are no child elements, the parent component retains its properties, i.e., they haven’t passed yet. Properties are assigned to the component after construction and before the connectedCallback() hook.
connectedCallback
The connectedCallback hook is called when the element is inserted into a document. This hook also flows from parent to child. You can use connectedCallback() to interact with a component’s environment. You can communicate with the current document or container and coordinate behavior with the environment.
It also helps perform initialization tasks, such as fetching data, setting up caches, or listening for events. Additionally, you can Subscribe and Unsubscribe from a Message Channel.
The component receives the initial properties when the connectedCallback() hook is called. The connectedCallback() hook has many firing instances.
领英推荐
For instance, the hook fires multiple times if you remove an element and then put it in a different location, like when you rearrange a list. If you only want it to run once, write code to prevent it from executing twice.
Read More
renderedCallback
The renderedCallback is called after every render of the component, the first and every consecutive one. A component is usually rerendered when the value of a property changes, and that property is used either directly in a component template or indirectly in the getter of a property used in a template.
The renderedCallback hook flows from child to parent.
This lifecycle hook is specific to Lightning Web Components; it isn’t from the HTML custom elements specification.
You can use renderedCallback() for the following:
render
If you wish to update the UI, you can use the render hook. It may be called before or after connectedCallback(). It’s rare to call render() in a component, as the primary use case of this hook is to render a template conditionally. It would help if you defined business logic to decide which template (HTML file) to use. The method must return a valid HTML template.
For example, imagine you have a component that can be rendered in two different ways, but you don’t want to mix the HTML in one file. Create multiple HTML files in the component bundle. Import them both and add a condition in the render() method to return the correct template depending on the component’s state.
Note: The render() method is not technically a lifecycle hook. It is a protected method on the LightningElement class. A hook usually tells you that something happened, and it may or may not exist on the prototype chain. The render() method must exist on the prototype chain.
disconnectedCallback
The disconnectedCallback hook is called when the element is removed from a document. This hook flows from parent to child. You can utilize disconnectedCallback() to clean up the work done in the connectedCallback(), like purging caches or removing event listeners. You can also use this hook to unsubscribe from a message channel.
Read More At Lifecycle Hooks in LWC: A Detailed Guide