Lifecycle Hooks are functions that are called automatically at a specific time designed to help you manage and manipulate the behavior of a component and its elements, such as when it's about to be created, updated, or destroyed.
These functions allow you to control and manage various aspects of your components at runtime.
Here is the list of all methods:
constructor
: helps LWCs get ready before they start working.connectedCallback
: called when the LWC is ready to be used and is displayed on the web page.disconnectedCallback
: called when the LWC is no longer being used and is removed from the web page.render
: this method allows selecting which template we want to show based on some conditions, and it is executed after the execution of connectedCallback
.renderedCallback
: helps the LWC know when it needs to be updated and show new information on the web page.errorCallback
: helps the LWC know when there's a problem and how to deal with it.<aside> ☝ You can find the latest official documentation here: https://lwc.dev/guide/lifecycle
</aside>
constructor()
method fires when a component instance is created.constructor()
.
super()
with no parameters. This call establishes the correct prototype chain and value for this
. Always call super()
before touching this
.return
statement inside the constructor body, unless it is a simple early return (return
or return this
).document.write()
or document.open()
methods.This example is illegal because it adds an attribute to the host element in the constructor()
.