ReactJS组件生命周期详述
前面已经写了一篇关于reactJS组件生命周期的博文,此篇博文是一个补充,增加了一些例子,有助于更好的理解reactJS组件。 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Keywords" content="关键词一,关键词二">
<meta name="Description" content="网站描述内容">
<meta name="Author" content="刘艳">
<title></title>
</head>
<body>
<div id = "example"></div>
<div id = "example2"></div>
</body>
</html>
<script src="build/jquery-1.11.2.min.js"></script>
<script src="build/react.js"></script>
<script src="build/react-dom.js"></script>
<script src="build/browser.min.js"></script>
<script type="text/babel"> var MyComponent = React.createClass({ getDefaultProps: function(){ console.log("获取实例的默认属性"); return{name: "Yvette"}; },getInitialState: function () { console.log("获取实例的初始状态"); return{will:true}; },componentWillMount: function () { console.log("组件即将被渲染到页面"); },handleClick: function(event){ this.setState({will: !this.state.will}); },componentDidMount: function(){ console.log("aaaa"); if(this.state.will){ $(this.refs.example).append("啦啦啦"); }else{ $(this.refs.example).append("郁闷"); } },render: function(){ console.log("render"); return( <div> <p ref = "example" onClick = {this.handleClick}>{this.props.name}未来{this.state.will ? "会" : "不会"}更好!</p> </div> ) } }); ReactDOM.render(<MyComponent/>,document.querySelector("#example")); ReactDOM.render(<MyComponent/>,document.querySelector("#example2")); </script>
运行中阶段能够使用的钩子函数(按照触发顺序): <body>
<div id = "example"></div>
<div id = "example2"></div>
</body>
</html>
<script src="build/jquery-1.11.2.min.js"></script>
<script src="build/react.js"></script>
<script src="build/react-dom.js"></script>
<script src="build/browser.min.js"></script>
<script type="text/babel"> var MyComponent = React.createClass({ getDefaultProps: function(){ console.log("获取实例的默认属性"); return{name: "Yvette"}; },componentDidMount: function(){ console.log("组件被渲染到页面之后"); $(this.refs.example).append("啦啦啦"); },render: function(){ console.log("render") return( <div> <p ref = "example" onClick = {this.handleClick}>{this.props.name}未来{this.state.will ? "会" : "不会"}更好!</p> </div> ); },componentWillReceiveProps: function(){ console.log("组件快要接收到属性"); },shouldComponentUpdate: function(){ console.log("是否需要更新"); return false; },componentWillUpdate: function(){ console.log("组件即将被更新"); },componentDidUpdate: function(){ console.log("组件更新被渲染到页面"); } }); ReactDOM.render(<MyComponent/>,document.querySelector("#example2")); </script>
运行结果如下: <body>
<div id = "example"></div>
<div id = "example2"></div>
</body>
</html>
<script src="build/jquery-1.11.2.min.js"></script>
<script src="build/react.js"></script>
<script src="build/react-dom.js"></script>
<script src="build/browser.min.js"></script>
<script type="text/babel">
var MyComponent = React.createClass({
getDefaultProps: function(){
console.log("获取实例的默认属性");
return{name: "Yvette"};
},getInitialState: function () {
console.log("获取实例的初始状态");
return{will:true};
},componentWillMount: function () {
console.log("组件即将被渲染到页面");
},handleClick: function(event){
this.setState({will: !this.state.will});
},componentDidMount: function(){
console.log("组件被渲染到页面之后");
//$(this.refs.example).append("啦啦啦");
},render: function(){
console.log("render")
return(
<div>
<p ref = "example" onClick = {this.handleClick}>{this.props.name}未来{this.state.will ? "会" : "不会"}更好!</p>
<span ref = "more">啦啦啦</span>
</div>
);
},componentWillReceiveProps: function(){
console.log("组件快要接收到属性");
},shouldComponentUpdate: function(){
console.log("是否需要更新");
return true;
},componentWillUpdate: function(){
console.log("组件即将被更新");
$(this.refs.example).css({"background": "#ccc","line-height":"30px"});
//this.setState({will: !this.state.will});//导致一个死循环
},componentDidUpdate: function(){
console.log("组件更新被渲染到页面");
if(this.state.will){
$(this.refs.more).html("啦啦啦");
}
else{
$(this.refs.more).html("郁闷");
}
}
});
ReactDOM.render(<MyComponent/>,document.querySelector("#example"));
ReactDOM.render(<MyComponent/>,document.querySelector("#example2"));
</script>
1、shouldComponentUpdate,必须返回true或false,直接返回,不会再触发后面的钩子函数。 销毁中阶段能够使用的钩子函数(按照触发顺序): <body>
<div id = "example"></div>
</body>
</html>
<script src="build/react.js"></script>
<script src="build/react-dom.js"></script>
<script src="build/browser.min.js"></script>
<script type="text/babel"> var style = { color: "red",border: "1px solid #000" }; var HelloWorld = React.createClass({ render: function(){ return <p>Hello,{this.props.name ? this.props.name : "World"}</p>; },componentWillUnmount: function(){ console.log("I will unmount"); } }); var HelloUniverse = React.createClass({ getInitialState: function(){ return {name: "Yvette"}; },handleChange: function (event) { if(event.target.value == "123"){ React.unmountComponentAtNode(document.querySelector("#example")) return; } this.setState({name: event.target.value}); },render: function(){ return( <div> <HelloWorld name = {this.state.name}></HelloWorld> <br/> <input type = "text" onChange = {this.handleChange} /> </div> ); } }); ReactDOM.render(<div style = {style}><HelloUniverse></HelloUniverse></div>,document.querySelector("#example")); </script>
当输入结果为123时,触发componentWillUnmount钩子函数。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |