{"id":2915,"date":"2017-11-14T17:30:17","date_gmt":"2017-11-14T09:30:17","guid":{"rendered":"http:\/\/www.daliane.com\/?p=2915"},"modified":"2017-11-14T17:40:00","modified_gmt":"2017-11-14T09:40:00","slug":"react_zu_jian_sheng_ming_zhou_qi","status":"publish","type":"post","link":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/react_zu_jian_sheng_ming_zhou_qi\/","title":{"rendered":"React \u7ec4\u4ef6\u751f\u547d\u5468\u671f"},"content":{"rendered":"<p style=\"font-size:13px;font-family:&quot;color:#333333;background-color:#FFFFFF;\">\n\t\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f\u53ef\u5206\u6210\u4e09\u4e2a\u72b6\u6001\uff1a\n<\/p>\n<ul style=\"color:#333333;font-family:&quot;background-color:#FFFFFF;\">\n<li style=\"font-size:13px;\">\n\t\tMounting\uff1a\u5df2\u63d2\u5165\u771f\u5b9e DOM\n\t<\/li>\n<li style=\"font-size:13px;\">\n\t\tUpdating\uff1a\u6b63\u5728\u88ab\u91cd\u65b0\u6e32\u67d3\n\t<\/li>\n<li style=\"font-size:13px;\">\n\t\tUnmounting\uff1a\u5df2\u79fb\u51fa\u771f\u5b9e DOM\n\t<\/li>\n<\/ul>\n<p style=\"font-size:13px;font-family:&quot;color:#333333;background-color:#FFFFFF;\">\n\t\u751f\u547d\u5468\u671f\u7684\u65b9\u6cd5\u6709\uff1a\n<\/p>\n<ul class=\"list\" style=\"color:#333333;font-family:&quot;background-color:#FFFFFF;\">\n<li style=\"font-size:13px;\">\n<p>\n\t\t\t<b>componentWillMount<\/b>&nbsp;\u5728\u6e32\u67d3\u524d\u8c03\u7528,\u5728\u5ba2\u6237\u7aef\u4e5f\u5728\u670d\u52a1\u7aef\u3002\n\t\t<\/p>\n<\/li>\n<li style=\"font-size:13px;\">\n<p>\n\t\t\t<b>componentDidMount<\/b>&nbsp;: \u5728\u7b2c\u4e00\u6b21\u6e32\u67d3\u540e\u8c03\u7528\uff0c\u53ea\u5728\u5ba2\u6237\u7aef\u3002\u4e4b\u540e\u7ec4\u4ef6\u5df2\u7ecf\u751f\u6210\u4e86\u5bf9\u5e94\u7684DOM\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7this.getDOMNode()\u6765\u8fdb\u884c\u8bbf\u95ee\u3002 \u5982\u679c\u4f60\u60f3\u548c\u5176\u4ed6JavaScript\u6846\u67b6\u4e00\u8d77\u4f7f\u7528\uff0c\u53ef\u4ee5\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u8c03\u7528setTimeout, setInterval\u6216\u8005\u53d1\u9001AJAX\u8bf7\u6c42\u7b49\u64cd\u4f5c(\u9632\u6b62\u5f02\u90e8\u64cd\u4f5c\u963b\u585eUI)\u3002\n\t\t<\/p>\n<\/li>\n<li style=\"font-size:13px;\">\n<p>\n\t\t\t<b>componentWillReceiveProps<\/b>&nbsp;\u5728\u7ec4\u4ef6\u63a5\u6536\u5230\u4e00\u4e2a\u65b0\u7684prop\u65f6\u88ab\u8c03\u7528\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728\u521d\u59cb\u5316render\u65f6\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002\n\t\t<\/p>\n<\/li>\n<li style=\"font-size:13px;\">\n<p>\n\t\t\t<b>shouldComponentUpdate<\/b>&nbsp;\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5728\u7ec4\u4ef6\u63a5\u6536\u5230\u65b0\u7684props\u6216\u8005state\u65f6\u88ab\u8c03\u7528\u3002\u5728\u521d\u59cb\u5316\u65f6\u6216\u8005\u4f7f\u7528forceUpdate\u65f6\u4e0d\u88ab\u8c03\u7528\u3002&nbsp;<br \/>\n\u53ef\u4ee5\u5728\u4f60\u786e\u8ba4\u4e0d\u9700\u8981\u66f4\u65b0\u7ec4\u4ef6\u65f6\u4f7f\u7528\u3002\n\t\t<\/p>\n<\/li>\n<li style=\"font-size:13px;\">\n<p>\n\t\t\t<b>componentWillUpdate<\/b>\u5728\u7ec4\u4ef6\u63a5\u6536\u5230\u65b0\u7684props\u6216\u8005state\u4f46\u8fd8\u6ca1\u6709render\u65f6\u88ab\u8c03\u7528\u3002\u5728\u521d\u59cb\u5316\u65f6\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002\n\t\t<\/p>\n<\/li>\n<li style=\"font-size:13px;\">\n<p>\n\t\t\t<b>componentDidUpdate<\/b>&nbsp;\u5728\u7ec4\u4ef6\u5b8c\u6210\u66f4\u65b0\u540e\u7acb\u5373\u8c03\u7528\u3002\u5728\u521d\u59cb\u5316\u65f6\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002\n\t\t<\/p>\n<\/li>\n<li style=\"font-size:13px;\">\n<p>\n\t\t\t<b>componentWillUnmount<\/b>\u5728\u7ec4\u4ef6\u4ece DOM \u4e2d\u79fb\u9664\u7684\u65f6\u5019\u7acb\u523b\u88ab\u8c03\u7528\u3002\n\t\t<\/p>\n<\/li>\n<\/ul>\n<p>\n\t<span><span style=\"font-size:13px;\"> <\/span><\/span>\n<\/p>\n<pre class=\"prettyprint lang-js\">var Hello = React.createClass({\r\n  getInitialState: function () {\r\n    return {\r\n      opacity: 1.0\r\n    };\r\n  },\r\n \r\n  componentDidMount: function () {\r\n    this.timer = setInterval(function () {\r\n      var opacity = this.state.opacity;\r\n      opacity -= .05;\r\n      if (opacity &lt; 0.1) {\r\n        opacity = 1.0;\r\n      }\r\n      this.setState({\r\n        opacity: opacity\r\n      });\r\n    }.bind(this), 100);\r\n  },\r\n \r\n  render: function () {\r\n    return (\r\n      &lt;div style={{opacity: this.state.opacity}}&gt;\r\n        Hello {this.props.name}\r\n      &lt;\/div&gt;\r\n    );\r\n  }\r\n});\r\n \r\nReactDOM.render(\r\n  &lt;Hello name=\"world\"\/&gt;,\r\n  document.body\r\n);<\/pre>\n<p>\n\t\u6240\u6709\u5173\u4e8e\u751f\u547d\u5468\u671f\u7684\n<\/p>\n<p><pre class=\"prettyprint lang-js\">import React, { Component } from 'react';\r\n\r\nclass LikeButton extends Component {\r\n    constructor(props) {\r\n        super(props);\r\n\r\n        \/\/ \u8bbe\u7f6e initial state\r\n        this.state = {\r\n            liked: { liked: false }\r\n        };\r\n\r\n        \/\/ ES6 \u7c7b\u4e2d\u51fd\u6570\u5fc5\u987b\u624b\u52a8\u7ed1\u5b9a\r\n        this.handleClick = this.handleClick.bind(this);\r\n    }\r\n    componentWillMount() {\r\n        console.log('Component WILL MOUNT!\u5728\u6e32\u67d3\u524d\u8c03\u7528,\u5728\u5ba2\u6237\u7aef\u4e5f\u5728\u670d\u52a1\u7aef')\r\n    }\r\n    componentDidMount() {\r\n        console.log('Component DID MOUNT!\u5728\u7b2c\u4e00\u6b21\u6e32\u67d3\u540e\u8c03\u7528\uff0c\u53ea\u5728\u5ba2\u6237\u7aef')\r\n    }\r\n    componentWillReceiveProps(newProps) {\r\n        console.log('Component WILL RECEIVE PROPS!\u5728\u7ec4\u4ef6\u63a5\u6536\u5230\u4e00\u4e2a\u65b0\u7684prop\u65f6\u88ab\u8c03\u7528')\r\n    }\r\n    shouldComponentUpdate(newProps, newState) {\r\n        console.log(\"shouldComponentUpdate\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5728\u7ec4\u4ef6\u63a5\u6536\u5230\u65b0\u7684props\u6216\u8005state\u65f6\u88ab\u8c03\u7528\");\r\n        return true;\r\n    }\r\n    componentWillUpdate(nextProps, nextState) {\r\n        console.log('Component WILL UPDATE!\u5728\u7ec4\u4ef6\u63a5\u6536\u5230\u65b0\u7684props\u6216\u8005state\u4f46\u8fd8\u6ca1\u6709render\u65f6\u88ab\u8c03\u7528');\r\n    }\r\n    componentDidUpdate(prevProps, prevState) {\r\n        console.log('Component DID UPDATE\u5728\u7ec4\u4ef6\u5b8c\u6210\u66f4\u65b0\u540e\u7acb\u5373\u8c03\u7528!')\r\n    }\r\n    componentWillUnmount() {\r\n        console.log('Component WILL UNMOUNT\u5728\u7ec4\u4ef6\u4ece DOM \u4e2d\u79fb\u9664\u7684\u65f6\u5019\u7acb\u523b\u88ab\u8c03\u7528!')\r\n    }\r\n    handleClick(event) {\r\n        this.setState({\r\n            liked: !this.state.liked\r\n        });\r\n        \/\/ this.state = { liked: !this.state.liked };\r\n    }\r\n    render() {\r\n\r\n        let text = this.state.liked ? '\u559c\u6b22' : '\u4e0d\u559c\u6b22';\r\n        return ( &lt; button onClick = { this.handleClick } &gt; { text } &lt; \/button&gt;);\r\n        }\r\n    }\r\n\r\n    export default LikeButton<\/pre>\n<\/p>\n<p>\n\t\n<\/p>\n<p>\n\t<span><span style=\"font-size:13px;\"> <\/span><\/span>\n<\/p>\n<p><\/p>\n<p>\n\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f\u53ef\u5206\u6210\u4e09\u4e2a\u72b6\u6001\uff1a Mounting\uff1a\u5df2\u63d2\u5165\u771f\u5b9e DOM Updating\uff1a\u6b63\u5728\u88ab\u91cd\u65b0\u6e32\u67d3 Unm&#8230;<\/p>\n","protected":false},"author":2,"featured_media":228,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[38],"tags":[196],"_links":{"self":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/2915"}],"collection":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/comments?post=2915"}],"version-history":[{"count":2,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/2915\/revisions"}],"predecessor-version":[{"id":2917,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/2915\/revisions\/2917"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/media\/228"}],"wp:attachment":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/media?parent=2915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/categories?post=2915"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/tags?post=2915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}