{"id":92156,"date":"2020-05-13T14:12:29","date_gmt":"2020-05-13T06:12:29","guid":{"rendered":"http:\/\/4563.org\/?p=92156"},"modified":"2020-05-13T14:12:29","modified_gmt":"2020-05-13T06:12:29","slug":"react-%e4%b8%ba%e4%bb%80%e4%b9%88%e8%a6%81%e6%b1%82%e6%95%b0%e6%8d%ae%e4%b8%8d%e5%8f%af%e5%8f%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=92156","title":{"rendered":"react \u4e3a\u4ec0\u4e48\u8981\u6c42\u6570\u636e\u4e0d\u53ef\u53d8"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  react \u4e3a\u4ec0\u4e48\u8981\u6c42\u6570\u636e\u4e0d\u53ef\u53d8               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kwoktung <\/span>  <span><i><\/i> 30<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u7c7b\u4f3c\u7684\u5e93 https:\/\/immerjs.github.io\/immer\/docs\/introduction \u548c \u5404\u79cd\u6559\u7a0b react \u8981\u6c42\u6570\u636e\u4e0d\u53ef\u53d8\uff1b\u6807\u5fd7\u6027\u7528\u6cd5 this.setState({ &#8230;state, a: &#8216;1&#8217; })<\/p>\n<p>\u4f46\u662f\u5176\u5b9e this.setState({ a: &#8216;1&#8217; })\u5c31\u53ef\u4ee5\u3002react \u672c\u8eab\u80cc\u81ea\u5df1\u8fdb\u884c merge \u64cd\u4f5c\u5417\u3002\u4f8b\u5982\u8fdb\u884c object.assign({}, state1, state2)\uff1b<\/p>\n<p>\u672c\u8d28\u4e0a setState \u53ea\u662f\u505a\u4e00\u4e2a\u201c\u810f\u6807\u8bb0\u201d\uff0c\u544a\u8bc9 react \u6211\u8fd9\u4e2a\u53ef\u80fd\u7ec4\u4ef6\u201c\u810f\u201d\uff0c\u53ef\u80fd\u9700\u8981\u5237\u65b0\u3002\u90a3\u4e48\u5e72\u4ec0\u4e48\u8fd8\u8981\u6c42\u6570\u636e\u4e0d\u53ef\u53d8\u554a\u3002<\/p>\n<p>\u6709\u5927\u4f6c\u543c\u4e00\u58f0\u5417\uff1f      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1238183\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : seki <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8bf4\u7684 setState \u7684\u5199\u6cd5\u6ca1\u4ec0\u4e48\u95ee\u9898<\/p>\n<p>\u6240\u8c13\u7684\u6570\u636e\u4e0d\u53ef\u53d8\u6307\u7684\u662f state \u91cc\u9762\u7684\u53d8\u91cf\u4e0d\u80fd\u88ab mutate<br \/>\u6bd4\u5982 state.foo \u662f\u4e00\u4e2a array\uff0c\u4e8e\u662f\u4e0b\u9762\u7684\u5199\u6cd5\u5c31\u662f\u6709\u95ee\u9898\u7684<\/p>\n<p>const { foo } = this.state<br \/>foo.push(1)<br \/>this.setState({ foo })                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1238184\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : randyo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/zh-hans.reactjs.org\/tutorial\/tutorial.html#why-immutability-is-important                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1238185\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shintendo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u4e3e\u7684\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u8ddf\u6570\u636e\u4e0d\u53ef\u53d8\u6ca1\u4ec0\u4e48\u5173\u7cfb\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1238186\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : otakustay <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             state \u662f\u4e2a\u6570\u636e\u7684\u5bb9\u5668\uff0c\u672c\u8eab\u4e0d\u662f\u6570\u636e\u3002state \u91cc\u9762\u7684\u6bcf\u4e00\u4e2a\u5c5e\u6027\u624d\u662f\u6570\u636e<br \/>\u5bb9\u5668\u6ca1\u5fc5\u8981\u4e0d\u53ef\u53d8\uff0c\u4f46\u5bb9\u5668\u91cc\u9762\u7684\u6570\u636e\u5fc5\u987b\u4e0d\u53ef\u53d8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1238187\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : roscoecheung1993 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u53ef\u53d8\u6570\u636e\u53ea\u9700\u8981\u4e00\u6b21\u6d45\u6bd4\u8f83\u5c31\u53ef\u4ee5\u786e\u5b9a\u6574\u68f5\u5b50\u6811\u662f\u5426\u9700\u8981\u66f4\u65b0\uff0cmerge \u89c6\u60c5\u51b5\u53ef\u80fd\u9700\u8981\u904d\u5386\u7ec4\u4ef6\u6811\u5230\u6bd4\u8f83\u6df1\u7684\u5c42\u7ea7\uff0c\u5728 state \u590d\u6742\u7684\u65f6\u5019\u4e0d\u53ef\u53d8\u6570\u636e\u4f1a\u6709\u6027\u80fd\u63d0\u5347\u3002\u5176\u4ed6\u597d\u5904\u8fd8\u5305\u62ec\u65b9\u4fbf\u505a\u5feb\u7167\uff0c\u65b9\u4fbf debug \u7b49\u7b49\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1238188\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kwoktung <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @roscoecheung1993 \u65b9\u4fbf\u505a\u5feb\u7167\u548c\u65b9\u4fbf debug \u600e\u4e48\u8bf4\uff1f\u6709\u4f8b\u5b50\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1238189\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : roscoecheung1993 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @kwoktung \u53c2\u8003\u4e8c\u7684\u94fe\u63a5\u548c react \u6587\u6863\u6027\u80fd\u4f18\u5316\u4e00\u8282\uff0c\u7b80\u5355\u6765\u8bf4\u6570\u636e\u4e0d\u53ef\u53d8\u7684\u7ed3\u679c\u662f\u6bcf\u6b21\u66f4\u65b0\u4f7f\u7528\u5168\u65b0\u7684\u4e00\u6574\u4e2a\u5bf9\u8c61\uff0c\u66f4\u65b9\u4fbf\u8bb0\u5f55                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>react \u4e3a\u4ec0\u4e48\u8981\u6c42\u6570\u636e\u4e0d\u53ef\u53d8 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/92156"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=92156"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/92156\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=92156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=92156"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=92156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}