onMounted
Overview
Section titled “Overview”The onMounted function allows you to register a callback function that will be executed when an app or component is mounted or initialized. This is particularly useful for performing setup tasks or side effects when a component becomes active.
Registering the onMounted Callback
Section titled “Registering the onMounted Callback”To register an onMounted callback, simply call the onMounted function and pass the desired callback function as an argument. The callback will be executed when the app or component is mounted.
import { createApp, html, useScope, onMounted } from 'regor'
const userRow = createComponent(() => ({ // Register an onMounted callback onMounted(() => { // Perform initialization tasks console.log('Component is mounted!') })}),html`<div></div>`)
createApp( useScope(() => { // Register an onMounted callback onMounted(() => { // Perform initialization tasks console.log('App is mounted!') }) return {} }),)Parameters
Section titled “Parameters”onMounted(required): A callback function that will be executed when the component or scope is mounted.
-
onMountedcallbacks can be useful for initializing state, fetching data, setting up event listeners, or any other task that should be performed when a component becomes active. -
Multiple
onMountedcallbacks can be registered, and they will be executed in the order they were registered. -
onMountedis often used in conjunction withonUnmountedto define cleanup tasks that should be performed when a component is unmounted.