Omi 生命周期

2018-05-10 11:45 更新

生命周期

Lifecycle methodWhen it gets called
componentWillMount / installbefore the component gets mounted to the DOM
componentDidMount / installedafter the component gets mounted to the DOM
componentWillUnmount / uninstall    prior to removal from the DOM                  
componentWillReceivePropsbefore new props get accepted
shouldComponentUpdatebefore render(). Return false to skip render
componentWillUpdate / beforeUpdatebefore render()
componentDidUpdate / afterUpdateafter render()

举个例子

class Timer extends Omi.Component {
    install () {
        this.data = {secondsElapsed: 0};
    }

    tick() {
        this.data.secondsElapsed++;
        this.update();
    }

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

    uninstall() {
        clearInterval(this.interval);
    }

    render () {
        return <div>Seconds Elapsed:<span> {{secondsElapsed}}</span></div>;
    }
}


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号