Palette
    Preparing search index...

    Palette Components are just custom web elements, in that they are standard JavaScript classes which extend the HTMLElement class.

    While a Component is rendered on a page, it will go through its "lifecycle," invoking lifecycle methods along the way. These methods are where the majority of your component logic will live.

    The Component lifecycle is:

    1. Creation: The element has been allocated but not yet mounted
    2. Initialization: The element has been attached to the DOM and is initializing
    3. Updates: Attribute changes, state changes, or manual requests have triggered an update
    4. Unmount: The component has been removed from the document and cleaned up

    Moving a component within the same task (e.g. reordering list rows) does not unmount it. If a component is removed and later re-attached (for example a child inside an ::if branch that is toggled off and on), it goes through Initialization again: script() runs again after the previous cycle's cleanup function and finalize(). Its state is preserved across the cycle.

    Each part of the lifecycle (aside from Creation) has associated methods which can be defined on the Component class. If not defined, they are simply ignored.

    // Initialization scripting for the component. Can return a cleanup function.
    script();

    // Directly before a re-render will occur
    beforeUpdate();

    // Directly after a re-render has completed
    afterUpdate();

    // Final lifecycle method called on removal
    finalize();

    // Receives errors from this component's lifecycle and unhandled errors from
    // child components. See the Error Handling guide.
    onError(error);

    It can be hard to keep track of what should go in which lifecycle method. Here's a table to help make that easier on ya:

    Method Lifecycle Use Case
    script() Initialization Primary setup, event listeners, API request kickoff
    beforeUpdate() Update DOM-stateful reads, pre-render cleanup
    afterUpdate() Update Post-render side effects
    finalize() Unmount Standard cleanup that always runs
    onError() Any Error boundary for this component and its children