Function withLifecycle

  • withLifecycle is a utility function that adds lifecycle hooks to a Cardboard tag.

    Will call handler.mounted when the element is added to the DOM.
    Then call handler.beforeUnmount before the element is removed from the DOM.
    Finally call handler.unmounted when the element is removed from the DOM.

    Parameters

    • tag: CTag
    • handler: AtLeastOne<{
          beforeUnmounted?: ((tag) => boolean | Promise<boolean>);
          mounted?: ((tag) => boolean | Promise<boolean>);
          unmounted?: ((tag) => void);
      }>

    Returns CTag

    Example

    const myTag = withLifecycle(
    div('Hello World'),
    {
    mounted: (tag) => {
    console.log('Mounted:', tag);
    return true; // or false to prevent mounting
    },
    unmounted: (tag) => {
    console.log('Unmounted:', tag);
    },
    beforeUnmount: (tag) => {
    console.log('Before Unmount:', tag);
    return true; // or false to prevent unmounting
    },
    }
    );

Generated using TypeDoc