{"id":307,"date":"2021-09-27T20:07:34","date_gmt":"2021-09-27T12:07:34","guid":{"rendered":"http:\/\/gjweb.top\/?p=307"},"modified":"2021-09-27T20:07:34","modified_gmt":"2021-09-27T12:07:34","slug":"%e7%bb%84%e4%bb%b6%e6%9b%b4%e6%96%b0%e6%9c%ba%e5%88%b6%e5%8f%8a%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=307","title":{"rendered":"\u7ec4\u4ef6\u66f4\u65b0\u673a\u5236\u53ca\u6027\u80fd\u4f18\u5316"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7ec4\u4ef6\u66f4\u65b0\u673a\u5236<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>setState()<\/strong> \u7684\u4e24\u4e2a\u4f5c\u7528\uff1a1.\u4fee\u6539 state 2.\u66f4\u65b0\u7ec4\u4ef6\uff08UI\uff09<\/li><li>\u8fc7\u7a0b\uff1a\u7236\u7ec4\u4ef6\u91cd\u65b0\u6e32\u67d3\u65f6\uff0c\u4e5f\u4f1a\u91cd\u65b0\u6e32\u67d3\u5b50\u7ec4\u4ef6\uff0c\u4f46\u53ea\u4f1a\u6e32\u67d3<strong>\u5f53\u524d\u7ec4\u4ef6\u7684\u6240\u6709\u5b50\u7ec4\u4ef6<\/strong><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/gjweb.top\/wp-content\/uploads\/2021\/09\/16327443321.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"852\" height=\"255\" data-original=\"http:\/\/gjweb.top\/wp-content\/uploads\/2021\/09\/16327443321.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-308\"\/><\/div><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6027\u80fd\u4f18\u5316<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u51cf\u8f7b state<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u53ea\u5b58\u50a8\u5404\u7ec4\u4ef6\u6e32\u67d3\u76f8\u5173\u7684\u6570\u636e\uff08\u6bd4\u5982\uff1acount\u3001\u5217\u8868\u6570\u636e\u3001loading\u7b49\uff09<\/li><li>\u6ce8\u610f\uff1a<strong>\u4e0d\u505a\u6e32\u67d3<\/strong>\u7684\u6570\u636e<strong>\u4e0d\u8981\u653e\u5728<\/strong> <strong>state<\/strong> \u4e2d<\/li><li>\u5bf9\u4e8e\u8fd9\u79cd\u9700\u8981\u5728\u591a\u4e2a\u65b9\u6cd5\u4e2d\u7528\u5230\u7684\u6570\u636e\uff0c\u5e94\u8be5\u653e\u5728 <strong>this<\/strong> \u4e2d<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Hello extends Component {<br> &nbsp; &nbsp;conponentDidMount(){<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ timer \u5b58\u50a8\u5728 this \u4e2d,\u800c\u4e0d\u662f state \u4e2d<br> &nbsp; &nbsp; &nbsp; &nbsp;this.timerId = setInterval(()=&gt;{},2000)<br> &nbsp;  }<br> &nbsp; &nbsp;componentWillUnmount(){<br> &nbsp; &nbsp; &nbsp; &nbsp;clearInterval(this.timerId)<br> &nbsp;  }<br> &nbsp; &nbsp;render(){...}<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3 ( shouldComponentUpdate )<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7ec4\u4ef6\u66f4\u65b0\u673a\u5236\uff1a\u7236\u7ec4\u4ef6\u66f4\u65b0\u4f1a\u5f15\u8d77\u5b50\u7ec4\u4ef6\u4e5f\u88ab\u66f4\u65b0<\/li><li>\u95ee\u9898\uff1a\u5b50\u7ec4\u4ef6\u6ca1\u6709\u4efb\u4f55\u53d8\u5316\u65f6\u4e5f\u4f1a\u91cd\u65b0\u6e32\u67d3<\/li><li>\u5982\u4f55\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6e32\u67d3\u5462\uff1f<\/li><li>\u89e3\u51b3\u65b9\u5f0f\uff1a\u4f7f\u7528\u94a9\u5b50\u51fd\u6570 <strong>shouldComonentUpdate(nexProps,nextState)<\/strong><\/li><li>\u4f5c\u7528\uff1a\u901a\u8fc7\u8fd4\u56de\u503c\u51b3\u5b9a\u8be5\u7ec4\u4ef6\u662f\u5426\u91cd\u65b0\u6e32\u67d3\uff0c\u8fd4\u56de true \u8868\u793a\u91cd\u65b0\u6e32\u67d3\uff0cfalse\u5219\u4e0d\u6e32\u67d3<\/li><li>\u89e6\u53d1\u65f6\u673a\uff1a\u66f4\u65b0\u9636\u6bb5\u7684\u94a9\u5b50\u51fd\u6570\uff0c\u7ec4\u4ef6\u91cd\u65b0\u6e32\u67d3\u524d\u6267\u884c\uff08shouldComponentUpdate &gt; render\uff09<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Hello extends Component {<br> &nbsp; &nbsp;shouldComponentUpdate(newProps,newState){<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ newProps.xxx  === this.props.xxx<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ newState.xxx === this.state.xxx<br> &nbsp; &nbsp; &nbsp; &nbsp;return false<br> &nbsp;  }<br> &nbsp; &nbsp;render(){return(...)}<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u7eaf\u7ec4\u4ef6<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u8bf4\u660e\uff1a\u7eaf\u7ec4\u4ef6\u5185\u90e8\u7684\u5bf9\u6bd4\u662f <strong>shallow compart<\/strong> \uff08\u6d45\u5c42\u5bf9\u6bd4\uff09<\/li><li>\u5bf9\u4e8e\u503c\u7c7b\u578b\u6765\u8bf4\uff1a\u6bd4\u8f83\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u540c\uff08\u76f4\u63a5\u590d\u5236\u5373\u53ef\uff09<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>let number = 0\r\nlet newnumber= number\r\nnewNumber = 2\r\nconsole.log(number === newNumber) \/\/ falsestate = {number : 0}\r\nsetState({\r\n   number: Math.floor(Math.rendom() * 3)\r\n})\r\n\/\/ PureComponent \u5185\u90e8\u5bf9\u6bd4\r\n\u6700\u65b0\u7684state.name === \u4e0a\u4e00\u6b21\u7684 state.number \/\/ false, \u91cd\u65b0\u6e32\u67d3\u7ec4\u4ef6<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\u6765\u8bf4\uff1a\u503c\u6bd4\u8f83\u5bf9\u8c61\u7684\u5f15\u7528\uff08\u5730\u5740\uff09\u662f\u5426\u76f8\u540c<\/li><li>\u6ce8\u610f\uff1a<strong>state \u6216 props \u4e2d\u5c5e\u6027\u503c\u4e3a\u5f15\u7528\u4e86\u7c7b\u578b\u65f6\uff0c\u5e94\u8be5\u521b\u65b0\u65b0\u6570\u636e\uff0c\u4e0d\u8981\u76f4\u63a5\u4fee\u6539\u539f\u6570\u636e<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6b63\u786e \u521b\u5efa\u65b0\u6570\u636e<br>const newObj = {...state.obj,number:2}<br>setState({ obj : newObj })<br>\u200b<br>\/\/ \u6b63\u786e \u521b\u5efa\u65b0\u6570\u636e<br>\/*\u4e0d\u8981\u7528\u6570\u7ec4\u7684push \/ unshift \u7b49\u76f4\u63a5\u4fee\u6539\u5f53\u524d\u6570\u7ec4\u7684\u65b9\u6cd5*\/<br>\/*\u800c\u5e94\u8be5\u7528 cocat \u6216 slice \u7b49API\u8fd4\u56de\u65b0\u6570\u7ec4*\/<br>this.setState({<br> &nbsp; &nbsp;list:&#91;...this.state.list,{\u65b0\u6570\u636e}]<br>})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ec4\u4ef6\u66f4\u65b0\u673a\u5236 setState() \u7684\u4e24\u4e2a\u4f5c\u7528\uff1a1.\u4fee\u6539 state 2.\u66f4\u65b0\u7ec4\u4ef6\uff08UI\uff09 \u8fc7\u7a0b\uff1a\u7236\u7ec4\u4ef6\u91cd\u65b0\u6e32 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-307","post","type-post","status-publish","format-standard","hentry","category-react"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=307"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/307\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}