Lifecycle

Lifecycle method When it gets called
install before the component gets mounted to the DOM
installed after the component gets mounted to the DOM
uninstall prior to removal from the DOM
beforeUpdate before update
afterUpdate after update (deprecated)
updated after update
beforeRender before render()
receiveProps parent element re-render will trigger it

For example:

  1. import { render, WeElement, define } from 'omi'
  2. define('my-timer', class extends WeElement {
  3. static observe = true
  4. data = {
  5. seconds: 0
  6. }
  7. tick() {
  8. this.data.seconds++
  9. }
  10. install() {
  11. this.interval = setInterval(() => this.tick(), 1000)
  12. }
  13. uninstall() {
  14. clearInterval(this.interval)
  15. }
  16. render() {
  17. return <div>Seconds: {this.data.seconds}</div>
  18. }
  19. })
  20. render(<my-timer />, 'body')