常建57

路漫漫其修远兮。。。

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

react学习笔记2-react组件生命周期

1.react生病周期

constructor
void componentWillMount()
void componentDidMount()
void componentWillReceiveProps(nextProps)
bool shouldComponentUpdate(nextProps, nextState)
void componentWillUpdate(nextProps, nextState)
void componentDidUpdate()
ReactElement render()
void componentWillUnmount()

2.在react中,触发render的方法
以下假设shouldComponentUpdate都是按照默认返回true的方式。
a.首次渲染Initial Render
b.调用this.setState (并不是一次setState会触发一次render,React可能会合并操作,再一次性进行render)
c.父组件发生更新(一般就是props发生改变,但是就算props没有改变或者父子组件之间没有数据交换也会触发render)
d.调用this.forceUpdate


initial render:
constructor->componentWillMount->render->componentDidMount->componentWillUnamount

父组件更新即调用render(有可能属性改变,有可能强制刷新) ->componentWillReceiveProps->shouldComponentUpdate->componentWillUpdate->render->componentDidUpdate->componentWillUnamount
this.setState()->shouldComponentUpdate->componentWillUpdate->render->componentDidUpdate>componentWillUnamount
this.forceUpdate->componentWillUpdate->render->componentDidUpdate>componentWillUnamount

 

 例子:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1">
        <style type="text/css">
    #todoapp ul {
      list-style-type: none;         
       list-style-type: none; /* Hides bullet points from todo list */
      }              
     #todo-list input.edit {
       display: none; /* Hides input box*/
     }
     #todo-list .editing label {
       display: none; /* Hides label text when .editing*/
     }    
     #todo-list .editing input.edit {
       display: inline; /* Shows input text box when .editing*/
     }    
        </style>

        <script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script>
    <script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script>
    <script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script>

    </head>
    <body>
        <div id='container'></div>


        <script type="text/babel">
                // ReactDOM.render(<h1>hello world</h1>,document.getElementById('container')); 

class LifeCycle extends React.Component {
    constructor(props) {
        super(props);
        alert("Initial render");
        alert("constructor");
        this.state = {str: "hello"};
    }

    componentWillMount() {
        alert("componentWillMount");
    }

    componentDidMount() {
        alert("componentDidMount");
    }

    componentWillReceiveProps(nextProps) {
        alert("componentWillReceiveProps");
    }

    shouldComponentUpdate() {
        alert("shouldComponentUpdate");
        return true;        // 记得要返回true
    }

    componentWillUpdate() {
        alert("componentWillUpdate");
    }

    componentDidUpdate() {
        alert("componentDidUpdate");
    }

    componentWillUnmount() {
        alert("componentWillUnmount");
    }

    setTheState() {
        let s = "hello";
        if (this.state.str === s) {
            s = "HELLO";
        }
        this.setState({
            str: s
        });
    }

    forceItUpdate() {
        this.forceUpdate();
    }

    render() {
        alert("render");
        return(
            <div>
                <span>{"Props:"}<h2>{parseInt(this.props.num)}</h2></span>
                <br />
                <span>{"State:"}<h2>{this.state.str}</h2></span>
            </div>
        );
    }
}

class Container  extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            num: Math.random() * 100
        };
    }

    propsChange() {
        this.setState({
            num: Math.random() * 100
        });
    }

    setLifeCycleState() {
        this.refs.rLifeCycle.setTheState();
    }

    forceLifeCycleUpdate() {
        this.refs.rLifeCycle.forceItUpdate();
    }

    unmountLifeCycle() {
        // 这里卸载父组件也会导致卸载子组件
        ReactDOM.unmountComponentAtNode(document.getElementById("container"));
    }

    parentForceUpdate() {
        this.forceUpdate();
    }

    render() {
        return (
            <div>
                <a href="javascript:;" className="weui_btn weui_btn_primary" onClick={this.propsChange.bind(this)}>propsChange </a>
                <a href="javascript:;" className="weui_btn weui_btn_primary" onClick={this.setLifeCycleState.bind(this)}>setState </a>
                <a href="javascript:;" className="weui_btn weui_btn_primary" onClick={this.forceLifeCycleUpdate.bind(this)}>forceUpdate </a>
                <a href="javascript:;" className="weui_btn weui_btn_primary" onClick={this.unmountLifeCycle.bind(this)}>unmount </a>
                <a href="javascript:;" className="weui_btn weui_btn_primary" onClick={this.parentForceUpdate.bind(this)}>parentForceUpdateWithoutChange </a>
                <LifeCycle ref="rLifeCycle" num={this.state.num}></LifeCycle>
            </div>
        );
    }
}

ReactDOM.render(
    <Container></Container>,
    document.getElementById('container')
);
        </script>
    </body>
</html>
View Code

 

转载来源

https://www.jianshu.com/p/4784216b8194

posted on 2018-02-23 22:12  常建57  阅读(134)  评论(0)    收藏  举报