LIFECYCLE HOOKS
Overview
Still provides with different types of Hooks not only when it comes to component Lifecycle but for additional specific context. Services (@Inject
annotation) and Proxies (@Proxy
annotation) also provide a hook (see this example) that can be used for components to subscribe so that they get triggered/notified when the service is loaded/ready.
Component Lifecycle Hooks/Special Methods
Essentially, in Still.js the component Lifecycle comprises 4 special methods (Hooks) as depicted in blue rectangles in the bellow diagram:
.
Examples Setup
The examples in this documentation/tutorials will be base in the bellow folder structure, Application Setup (app-setup.js
) and routes metadata (route.map.js
)
This is the where Application context aspects are setup. This file is in the root folder. | |
---|---|
The stOnRender()
Special method (Hook)
This example takes into consideration the above folder structure, app and routing setup.
This hook runs immediately after the component is instantiated. While the component isn't fully ready yet, it's suitable for actions like making API calls or showing a loading spinner.
This component is placed in the app/base-components/ folder | |
---|---|
The stAfterInit()
Special method (Hook)
This example takes into consideration the above folder structure, app and routing setup.
This takes place whenever the component was render and it's totally available for usage, in this stage we can for example access to DOM tree of such component.
The stOnUpdate()
Special method (Hook)
This example takes into consideration the above folder structure, app and routing setup.
This hook takes place everytime a component state gets changed since this is the change that reflects the UI. Component update triggers either the component updates itself or by another component.
The stOnUnload()
Special method (Hook)
This example takes into consideration the above folder structure, app and routing setup.
It takes place especially when destroying a component, which happens when navigating from one component or page to another. A basic use case for it would be if we need to clear some data in a global store (Service).
The stWhenReady()
Special method (Hook)
This example takes into consideration the above folder structure, app and routing setup.
This is a hook that works the same way as the stAfterInit()
, the only different is that it's not called in the class level but inside a method in the class (e.g. constructor
). A use case for this hook would be when the implementing component needs to run something on top of another unrelated (nor sibling or child) component gets loaded/rendered since it would never have controle over it.