Skip to content

Lifecycle

Usage

Lifecycle methodWhen it gets called
installbefore the component gets mounted to the DOM
installedafter the component gets mounted to the DOM
readywhen the component's properties, events, etc. are prepared and ready to use. This typically occurs asynchronously after the installed lifecycle
uninstallprior to removal from the DOM
beforeUpdatebefore update
updatedafter update
beforeRenderbefore render()
renderedafter render(),the first parameter is the virtual DOM, which can be changed to change the rendering result
receivePropsparent element re-render will trigger it, return false will prevent update action

For example:

tsx
import { render, Component, tag } from 'omi'

@tag('my-timer')
class MyTimer extends Component {
  data = {
    seconds: 0
  }

  tick() {
    this.data.seconds++
    this.update()
  }

  install() {
    this.interval = setInterval(() => this.tick(), 1000)
  }

  uninstall() {
    clearInterval(this.interval)
  }

  render() {
    return <div>Seconds: {this.data.seconds}</div>
  }
}

render(<my-timer />, 'body')
import { render, Component, tag } from 'omi'

@tag('my-timer')
class MyTimer extends Component {
  data = {
    seconds: 0
  }

  tick() {
    this.data.seconds++
    this.update()
  }

  install() {
    this.interval = setInterval(() => this.tick(), 1000)
  }

  uninstall() {
    clearInterval(this.interval)
  }

  render() {
    return <div>Seconds: {this.data.seconds}</div>
  }
}

render(<my-timer />, 'body')

Listening in Component Tags

tsx
import { render, Component, tag } from 'omi'

@tag('my-element')
class MyElement extends Component {
  render() {
    return <div>el</div>
  }
}

@tag('my-app')
class MyElement extends Component {
  render() {
    return (
      <my-element
        onInstalled={
          (evt: CustomEvent) => {
            updateMenu(evt.detail)
          }}>
      </my-element>
    )
  }
}
render(<my-timer />, 'body')
import { render, Component, tag } from 'omi'

@tag('my-element')
class MyElement extends Component {
  render() {
    return <div>el</div>
  }
}

@tag('my-app')
class MyElement extends Component {
  render() {
    return (
      <my-element
        onInstalled={
          (evt: CustomEvent) => {
            updateMenu(evt.detail)
          }}>
      </my-element>
    )
  }
}
render(<my-timer />, 'body')

Component users can listen to the lifecycle events of the component on the component tag, as in the example above, where the user can listen to the installed event of the component, and evt.detail is the component my-element itself.