zoukankan      html  css  js  c++  java
  • React组件生命周期-正确执行初始化阶段的函数

    一、

    二、代码

    <!DOCTYPE html>
    <html lang="zh-cn">
    
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    
    <body>
        <script src="./jquery-2.1.4.min.js"></script>
        <script src="./react-0.13.2/build/react.js"></script>
        <script src="./react-0.13.2/build/JSXTransformer.js"></script>
        <script type="text/jsx">
            $(function(){
                var count = 0;
                var style = {
                    color : "red",
                    border: "1px #000 solid",
                };
                var HelloWorld = React.createClass({
                    getDefaultProps: function(){
                        console.log("getDefaultProps1");
                        return {name:'tony'};
                    },
                    getInitialState: function(){
                        console.log("getInitialState2");
                        return {myCount : count++, reayd : false};
                    },
                    componentWillMount:function(){
                        console.log("componentWillMount3");
                        this.setState({ready: true});
                    },
                    render: function(){
                        console.log("render4");
                        return <p ref="childp">Hello,{this.props.name ? this.props.name : "World"}<br/>{""+this.state.ready}{this.state.myCount}</p>
                    },
                    componentDidMount:function(){
                        console.log("componentDidMount5");
                        $(React.findDOMNode(this)).append("surprise!!");
                    }
                });
                React.render(<div style={style}><HelloWorld></HelloWorld><br/><HelloWorld></HelloWorld></div>, document.body);
            });
    
        </script>
    </body>
    
    </html>

    结果:

  • 相关阅读:
    js中的字符串
    JSOP
    jq总结1
    jq总结
    诗和远方-志
    诗和远方-感
    js判断变量是否为空字符串、null、undefined
    判断js对象是否为空
    诗和远方-悟
    深复制
  • 原文地址:https://www.cnblogs.com/shamgod/p/5052673.html
Copyright © 2011-2022 走看看