setNewnumber 用于更新 state。所有生命周期在 Content 组件中

2017-07-17 16:48:00
admin
原创
401
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>React 实例</title>
    <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="example"></div>

    <script type="text/babel">
    var Button = React.createClass({
      getInitialState: function() {
        return {
          data:0
        };
      },
      setNewNumber: function() {
        this.setState({data: this.state.data + 1})
      },
      render: function () {
          return (
             <div>
                <button onClick = {this.setNewNumber}>INCREMENT</button>
                <Content myNumber = {this.state.data}></Content>
             </div>
          );
        }
    })
    var Content = React.createClass({
      componentWillMount:function() {
          console.log('Component WILL MOUNT!')
      },
      componentDidMount:function() {
           console.log('Component DID MOUNT!')
      },
      componentWillReceiveProps:function(newProps) {
            console.log('Component WILL RECEIVE PROPS!')
      },
      shouldComponentUpdate:function(newProps, newState) {
            return true;
      },
      componentWillUpdate:function(nextProps, nextState) {
            console.log('Component WILL UPDATE!');
      },
      componentDidUpdate:function(prevProps, prevState) {
            console.log('Component DID UPDATE!')
      },
      componentWillUnmount:function() {
             console.log('Component WILL UNMOUNT!')
      },

        render: function () {
          return (
            <div>
              <h3>{this.props.myNumber}</h3>
            </div>
          );
        }
    });
      ReactDOM.render(

         <div>
            <Button />
         </div>,
        document.getElementById('example')
      );
    </script>
  </body>
</html>
文章分类
联系我们
电话: 18902328227
Email: 2689701946@qq.com
QQ: 2689701946
微信: 18902328227
地址: 广州番禺区繁华路
  • 在线客服
  • 关注微信
  • 联系电话
    • 欢迎咨询
    • 欢迎加入QQ群 303257430
  • 公众号:源宝网络
    • 联系电话及微信号 18902328227