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

    一、

    二、

     1 <!DOCTYPE html>
     2 <html lang="zh-cn">
     3 
     4 <head>
     5     <meta charset="UTF-8">
     6     <title>Document</title>
     7 </head>
     8 
     9 <body>
    10     <script src="./jquery-2.1.4.min.js"></script>
    11     <script src="./react-0.13.2/build/react.js"></script>
    12     <script src="./react-0.13.2/build/JSXTransformer.js"></script>
    13     <script type="text/jsx">
    14         $(function(){
    15             var style = {
    16                 color : "red",
    17                 border: "1px #000 solid",
    18             };
    19             var HelloWorld = React.createClass({
    20                 componentWillReceiveProps: function (newProps) {
    21                     console.log("componentWillReceiveProps 1");
    22                     console.log(newProps);
    23                 },
    24                 shouldComponentUpdate: function () {
    25                     console.log("shouldComponentUpdate 2");
    26                     return true;
    27                 },
    28                 componentWillUpdate: function () {
    29                     console.log("componentWillUpdate 3");
    30                 },
    31                 render: function () { 
    32                     console.log("render 4");
    33                     return <p>Hello, {this.props.name ? this.props.name : "World"}</p>;
    34                 },
    35                 componentDidUpdate: function() {
    36                     $(React.findDOMNode(this)).append("surprise!");
    37                 },
    38             });
    39             var HelloUniverse = React.createClass({
    40                 getInitialState: function(){
    41                     return {name:''};
    42                 },
    43                 handleChange: function(event){
    44                     this.setState({name: event.target.value});
    45                 },
    46                 render: function(){
    47                     return <div>
    48                     <HelloWorld name={this.state.name}></HelloWorld>
    49                     <br/>
    50                     <input type="text" onChange={this.handleChange} />
    51                     </div>
    52                 },
    53             });
    54             React.render(<div style={style}><HelloUniverse></HelloUniverse></div>, document.body);
    55         });
    56 
    57     </script>
    58 </body>
    59 
    60 </html>

    结果:

  • 相关阅读:
    P1067 多项式输出(模拟水题)
    A. The Fair Nut and Elevator(暴力)
    A. The Fair Nut and Elevator(暴力)
    Knight Tournament (set)
    jquery怎么添加多个类名
    jquery对类的操作,添加,删除,点击添加,再点击删除
    jquery操作css样式的方法
    jquery浅复制和深复制区别
    TS 三种函数的定义方式
    ES7及ES8新特性
  • 原文地址:https://www.cnblogs.com/shamgod/p/5052741.html
Copyright © 2011-2022 走看看