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

舉個(gè)例子

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>;
    }
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)