componentWillMount:组件出现前,dom还没有渲染到html文档里面;
componentDidMount:件渲染完成 已经出现在dom文档里;
componentWillUnmount:说该事件在组件即将被移除dom时会触发;
React 生命周期 demo
首先引入相关的js
<script type="text/javascript" src="react.js"></script>
<script type="text/javascript" src="JSXTransformer.js"></script>
<script type="text/jsx"> var Timer=React.createClass({ getInitialState:function(){ return {secondsElapsed:0} }, tick:function(){ this.setState({secondsElapsed:this.state.secondsElapsed+1}); }, componentDidMount:function(){ this.interval=setInterval(this.tick,1000); }, componentWillUnmount:function(){ clearInterval(this.interval); }, render:function(){ return (<div>timer:{this.state.secondsElapsed}</div>) } }); React.render(<Timer />,document.body) </script>
时间: 2024-10-06 16:11:33