阅读(1758) (38)

Omi 生命周期

2017-02-20 11:23:23 更新

生命周期

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