React生命周期详解

2018-01-20  本文已影响0人  silly鸿

这篇文章我们将讨论React组件的生命周期

一、组件的生命周期分成三个状态:
二、生命周期方法:
Mounting
Updating
Unmounting

三、在渲染前可以使用的部分API

四、Demo

demo1

    <script type="text/jsx">
        var MessageBox = React.createClass({
            getDefaultProps:function(){
                console.log('getDefaultProps');
                return {};
            },
            getInitialState:function(){
                console.log('getInitialState');
                return {
                    count: 0,
                }
            },
            componentWillMount:function(){
                console.log('componentWillMount');

            },
            componentDidMount:function(){
                console.log('componentDidMount')
                var self = this;

                this.timer = setInterval(function(){
                    self.setState({
                        count: self.state.count + 1,
                    })
                },1000);
                console.log(this.getDOMNode(), this.state.count);
            },
            componentWillUnmount: function(){
                alert('you are tring to kill me !! ')

                clearInterval(this.timer);

            },
            killMySelf: function(){
                React.unmountComponentAtNode(  document.getElementById('app') );
            },
            render:function(){  
                console.log('渲染')
                return ( 
                    <div>
                        <h1 > 计数: {this.state.count}</h1> 
                        <button onClick={this.killMySelf}>卸载掉这个组件</button>
                        <Submessage/>
                    </div>
                )
            }
        });

        var Submessage = React.createClass({
            render:function(){
                return (
                    <h3>写代码很有意思</h3>
                )
            }
        });


        var messageBox = React.render( <MessageBox/>, 
            document.getElementById('app')
        )


    </script>

demo2

    <script type="text/jsx">
        var MessageBox = React.createClass({
            getInitialState:function(){
                return {
                    count: 0,
                }
            },
            getDefaultProps:function(){
            },
            shouldComponentUpdate:function(nextProp,nextState){
                console.log('shouldComponentUpdate');
                if(nextState.count > 10) return false;

                return true;
            },
            componentWillUpdate:function(nextProp,nextState){
                console.log('componentWillUpdate');
            },
            componentDidUpdate:function(){
                console.log('componentDidUpdate');
            },
            killMySelf: function(){
                React.unmountComponentAtNode(  document.getElementById('app') );
            },
            doUpdate:function(){
                this.setState({
                    count: this.state.count + 1,
                });
            },
            render:function(){  
                console.log('渲染')
                return ( 
                    <div>
                        <h1 > 计数: {this.state.count}</h1> 
                        <button onClick={this.killMySelf}>卸载掉这个组件</button>
                        <button onClick={this.doUpdate}>手动更新一下组件(包括子组件)</button>
                        <Submessage count={this.state.count}/>
                    </div>
                )
            }
        });

        var Submessage = React.createClass({
            componentWillReceiveProps:function(nextProp){
                console.log('子组件将要获得prop');
                
            },
            shouldComponentUpdate:function(nextProp,nextState){
                if(nextProp.count> 5) return false;
                return true;
            },
            render:function(){
                return (
                    <h3>当前计数是:{this.props.count}</h3>
                )
            }
        });


        var messageBox = React.render( <MessageBox/>, 
            document.getElementById('app')
        )
    </script>
上一篇 下一篇

猜你喜欢

热点阅读