• react生命周期


    import ReactDom from 'react-dom';
    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() {
            // 这里卸载父组件也会导致卸载子组件
            React.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('root')
    );
    

      

  • 相关阅读:
    jQuery 在 IE 上 clone checkbox 的問題。
    C/C++ typedef用法
    C++继承
    map常用操作
    C++ JsonCpp 使用(含源码下载)
    string常用操作
    C++虚函数
    STL容器迭代过程中删除元素技巧(转)
    关于IE下用HTTPS无法下载/打开文件(转)
    C++STL概览
  • 原文地址:https://www.cnblogs.com/1zhangwenjing/p/13630553.html
Copyright © 2020-2023  润新知