ITPub博客

首页 > 应用开发 > > React SetInterval 无法清理

React SetInterval 无法清理

作者:cylong 时间:2019-11-05 17:39:37 0 删除 编辑

<!DOCTYPE html>

<html>

  <head>

    <meta charset="UTF-8" />

    <title>Hello React2!</title>

    <script src="../build/react.js"></script>

    <script src="../build/react-dom.js"></script>

    <script src="../build/browser.min.js"></script>

  </head>

  <body>

    <div id="example"></div>

    <script type="text/babel">

    var FatherControl = React.createClass({

        getInitialState:function(){

            return{

                isStop:false,

                isNum:1

            };

        },

        onChildChanged:function(newState){

            this.setState({

                isStop:newState     

            });

            var dd;

            this.timer = setInterval(()=>{

                    var eeNum = this.state.isNum;

                    this.setState({isNum:eeNum+1});

                    if(!this.state.isStop){

                        console.log('this is stop');

                        console.log(this.timer); //这里可以取到这个timer的

                        clearInterval(this.timer);

                        this.timer = undefined;

                    }

                },1000);

        },

        componentWillUnMount:function(){      //这个测试用的,可以忽略

            clearInterval(this.timer)

        }, 

        numberAdd:function(){                 //这个也是测试用的,也可以忽略

            var ee=0

            ee = setInterval(function(){

                         var eeNum = this.state.isNum;

                         this.setState({isNum:eeNum+1})

                     }.bind(this),1000);

                    return function(){

                        this.ee;

                    }

        },

        render:function(){

            return (

                <div>

                    <ImgPanel showvalue={this.state.isNum}></ImgPanel>

                    <MyButton callbackParent={this.onChildChanged}>Start</MyButton>

                </div>

            )

        }

    })

    var MyButton = React.createClass({

        getInitialState:function(){

            return {

                isStop:this.props.onclick

            };

        },

        funClick:function(){

            var newState = !this.state.isStop;

            this.setState({

                isStop:newState

            });

            this.props.callbackParent(newState);

        },

        render:function(){

            var checked = this.state.checked;

            return(

              <button onClick={this.funClick}>Start</button>  

            )

        }

    })

    var ImgPanel = React.createClass({

        render:function(){

            return(

                <h3>{this.props.showvalue}</h3>

            )

        }

    });

    ReactDOM.render(

        <FatherControl/>,

        document.getElementById('example')

    );

    </script>

  </body>

</html>

现在在练习react组件部分的交互,FatherControl是父组件,MyButton和ImgPanel是子组件,ImgPanel是一个计数的功能,点击MyButton时候可以启动或者停止。刚开始练习,用的是旧版本的React...

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2005/viewspace-3140346/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    106
  • 访问量
    1362