Skip to content

Lifecycle

Lifecycle methodWhen it gets called
installbefore the component gets mounted to the DOM
installedafter the component gets mounted to the DOM
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')