qwik-docs
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    [![hackmd-github-sync-badge](https://hackmd.io/bQNLF9lySvyclywmM_Bd5g/badge)](https://hackmd.io/bQNLF9lySvyclywmM_Bd5g) # Lifecycle Hooks 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`. 1. `Void`: Component does not exist. Nothing has been created yet. This is equivalent to an object not being instantiated or an object not existing. 2. `Hydrated`: Component exists in VM heap and can be passed around as a reference. This is equivalent to how developers normally think of objects. 3. `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. ## A Typical lifecycle of a component in an SSR scenario. 1. Component is created on the server. It is in the `Hydrated` state and can be passed around by reference (the normal way of passing objects in JS.) 2. Server completes rendering and `dehydrate’s all of the components. This serializes all of the component states into the DOM attributes. Once the state is serialized in the DOM the server can convert the DOM into HTML and send the HTML to the client. 3. At this point, the VM no longer has a reference to the component instances. However, it would be incorrect to say that the component no longer exists. Instead, the component is in a `dehydrated` state. It is somewhere between non-existing and fully existing. 4. Client receives the HTML and turns it back to DOM. The DOM contains the component’s state, but the component is not yet hydrated. 5. Some action is performed which requires that the component is fully hydrated. It is at this point that the component can be re-created. Obviously, from the reference point, the object on the server and on the client are different instances. But logically, we can say that it is the same component. 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. 1. A logic component is created when the parent component’s render function creates a `<MyComp>` node. 2. The result of executing the parent’s component JSX is that `<my-comp>` host-element is created in the DOM, and the`<MyComp>`’s view is scheduled for rendering. 3. Before rendering can start, the component instance needs to be created. This is equivalent to `new QComponent()`. The newly created `QComponent` is missing the private and transient state, and so it fires `[OnMount]` (and a bit later `[OnHydrate]`) 4. `[OnMount]`: Allows the `[OnMount]` hook to create the state of the component. 5. The new `STATE` is assigned into `QComponent`. This allows the `[OnHydrate]` hook to run. 6. `[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. 7. The new `TRANSIENT` state is assigned to `QComponent`. At this point, the component is fully rehydrated and can be used for rendering or event handling. 8. `[OnRender]`: This invokes the `MyComp’s render function, which produces JSX nodes to be reconciled against the DOM. 9. The result of `[OnRender]` and reconciliation is that the `<my-comp>` host-element now contains `MyComp’s view fully rendered.. 10. `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. 11. `[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. 12. Qwik serializes the `STATE` of the component into the DOM. At this point, the `QComponent` is released and is available for garbage collection. 13. After `dehydrate()` completes, the DOM can be serialized to HTML and sent to the client. 14. The client receives the HTML and deserializes it into DOM. 15. The deserialized DOM contains the `<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. 16. If a component is needed, it can go through a rehydration process. First, the component’s state is parsed from the DOM and passed to the `QComponent` 17. A new `QComponent` is created, and the deserialized state is assigned to it. 18. `[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. 19. The new `TRANSIENT` state is assigned to the `QComponent`. At this point, the `QComponent` is ready to be used in rendering. 20. `[OnRender]`: On render, the method can execute, which can create new JSX nodes. 21. The rendered DOM is updated to reflect the changes from `<MyComp>`. The update process does not force child or parent components to be re-rendered unless the update changes props of those components. 22. At some point, the parent component removes the `<MyComp>` from its JSX tree. This triggers the destroy process. 23. The DOM is updated, and `<my-comp>` is removed. 24. `[OnUnmount]`: lifecycle hook is invoked to let the component know that it is being removed. 25. `[OnDehydrate]`: lifecycle hook is invoked to clean up the transient state of the component.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully