Skip to content

生命周期

Lifecycle methodWhen it gets called
install准备插入到文档
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')