Learn More →
Describes component lifecycle hooks.
In typical development, when discussing object lifespan, it is clear that objects either exist or they do not. Either an object has been instantiated, or it has not yet been instantiated (or it has been instantiated and has since been garbage collected. Hence it no longer exists.)
When discussing the lifespan of a Qwik component, it is necessary to expand the definition into
three states: Void
, dehydrated
, and Hydrated
.
Void
: Component does not exist. Nothing has been created yet. This is equivalent to an object not being instantiated or an object not existing.Hydrated
: Component exists in VM heap and can be passed around as a reference. This is equivalent to how developers normally think of objects.Dehydrated
: An in-between state between Void
and Hydrated
. A component has been created, but it is not represented in the VM heap as an actual object which can be passed around as a reference. In this state, the component state is serialized in the DOM/HTML but does not have the VM heap representation.Hydrated
state and can be passed around by reference (the normal way of passing objects in JS.)dehydrated
state. It is somewhere between non-existing and fully existing.For these reasons, it is important to differentiate between a logical component and a component instance. A logical component is a component that can span creation on the server and execution on the client. A logical component survives dehydration/rehydration events (a component instance does not.)
To describe the whole lifecycle of the component, refer to the diagram and explanation below.
logical || || private || transient || component ||
component || DOM || state || state || instance ||
JSX || || STATE || TRANSIENT || (VM ref) ||
======================================================================
(1)
<MyComp> (2)
|| <my-comp/> (3) new
|| || QComponent()
|| || new (4) |
|| || STATE() <---------[OnMount]----------- |
|| || || |
|| || || - - - - - - - (5) - - - - - - -> ||
|| || || new (6) ||
|| || || TRANSIENT() <---[OnHydrate]--- ||
|| || || || ||
|| || || || - - - - - -(7)- - - - > |||
|| || || || |||
|| || || || (8) |||
|| <my-comp> <=======================[OnRender]========= |||
|| (9) <view/> || || |||
|| </my-comp> || || |||
|| || || || |||
|| || || (10) || |||
---------------------------- dehydrate(document) -----------------------+
|| || || || ||| |
|| || || || (11) ||| |
|| || (12) || XX <-----[OnDehydrate]---- XXX <-+
|| <my-comp {STATE}> <-- XX (13)|
|| =================== Serialized to HTML ===================== <-+
|| == (14) HTTP ==
|| =================== Deserialize from HTML =================
|| <my-comp {STATE}>
|| <view/> (15)
|| </my-comp>
|| || (16) (17) new
|| || JSON QComponent()
|| || - - - - -> (parse) - - - - - - - - - - - - - - >||
|| || || ||
|| || || new (18) ||
|| || || TRANSIENT() <---[OnHydrate]--- ||
|| || || || ||
|| || || || - - - - - (19) - - - -> |||
|| || || || |||
|| || || || |||
|| || || || (20) |||
|| <my-comp> <=======================[OnRender]========= |||
|| (21) <view/> || || |||
|| </my-comp> || || |||
|| || || || |||
(removed) || || || (24) |||
(22) +-->(removed) ---------------------------[OnUnmount]----> |||
(23) || || (25) |||
XX<------XX <-----[OnDehydrate]---- XXX
Please match the numbers in the diagram to the explanation below.
<MyComp>
node.<my-comp>
host-element is created in the DOM, and the<MyComp>
’s view is scheduled for rendering.new QComponent()
. The newly created QComponent
is missing the private and transient state, and so it fires [OnMount]
(and a bit later [OnHydrate]
)[OnMount]
: Allows the [OnMount]
hook to create the state of the component.STATE
is assigned into QComponent
. This allows the [OnHydrate]
hook to run.[OnHydrate]
: Responsible for creating TRANSIENT
state. A transient state is a state which can’t be serialized (ie. promises, observables, closures, streams.) It is separated from [OnMount]
because [OnMount]
runs only once for the logical component. The application needs a way to be notified every time the component is deserialized.TRANSIENT
state is assigned to QComponent
. At this point, the component is fully rehydrated and can be used for rendering or event handling.[OnRender]
: This invokes the `MyComp’s render function, which produces JSX nodes to be reconciled against the DOM.[OnRender]
and reconciliation is that the <my-comp>
host-element now contains `MyComp’s view fully rendered..dehydrate()
: At some point, the server determines that the SSR is finished and the rendered applications should be sent to the client. The first step is to serialize all of the data into the DOM. This method locates all of the components and triggers the [OnDehydrate]
hook.[OnDehydrate]
is responsible for doing the reverse of [OnHydrate]
. The method is
responsible for releasing any resources which the [OnHydrate]
acquired and which are stored in TRANSIENT
state.STATE
of the component into the DOM. At this point, the QComponent
is released and is available for garbage collection.dehydrate()
completes, the DOM can be serialized to HTML and sent to the client.<my-comp {STATE}>
element along with its serialized state. The components are deserialized lazily. Only when QComponent
instance is needed does it go through the deserialization process.QComponent
QComponent
is created, and the deserialized state is assigned to it.[OnHydrate]
: [OnHydrate]
hook runs which creates a transient state for the component. This is also a good place to recreate any non-serializable objects, such as promises, observables, closures, and streams.TRANSIENT
state is assigned to the QComponent
. At this point, the QComponent
is ready to be used in rendering.[OnRender]
: On render, the method can execute, which can create new JSX nodes.<MyComp>
. The update process does not force child or parent components to be re-rendered unless the update changes props of those components.<MyComp>
from its JSX tree. This triggers the destroy process.<my-comp>
is removed.[OnUnmount]
: lifecycle hook is invoked to let the component know that it is being removed.[OnDehydrate]
: lifecycle hook is invoked to clean up the transient state of the component.