Skip to content

生命周期 Lifecycle

用法

Lifecycle methodWhen it gets called
install准备插入到文档
installed插入到文档之后
ready组件的属性、事件等已准备好并可以使用时触发,在 installed 之后异步执行
uninstall从文档中移除
beforeUpdateupdate 之前
updatedupdate 之后
beforeRenderrender() 之前
renderedrender() 之后,第一个参数是虚拟DOM,可以改变它改变渲染结果
receiveProps父元素重新渲染触发,返回 false 可阻止更新

举个例子:

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

@tag('my-timer')
class MyTimer extends Component {

  seconds = 0

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

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

  uninstall() {
    clearInterval(this.interval)
  }

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

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

@tag('my-timer')
class MyTimer extends Component {

  seconds = 0

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

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

  uninstall() {
    clearInterval(this.interval)
  }

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

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

组件标签中监听

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')

组件的使用者可以在组件标签上监听组件的生命周期事件,如上面的例子,组件的使用者可以监听组件的 installed 事件,其中 evt.detail 就是组件 my-element 本身。