{"id":162573,"date":"2020-10-01T16:53:20","date_gmt":"2020-10-01T08:53:20","guid":{"rendered":"http:\/\/4563.org\/?p=162573"},"modified":"2020-10-01T16:53:20","modified_gmt":"2020-10-01T08:53:20","slug":"react-hooks-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=162573","title":{"rendered":"react hooks \u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  react hooks \u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : QGabriel <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        useEffect( () =&gt; {<br \/>const getStatus = async () =&gt; {<br \/>let res = await queryMedical({id: null})<br \/>console.log(1, &#8216;formData&#8217;, formData)<br \/>setFormData(res)<br \/>console.log(2, &#8216;formData&#8217;, formData)<br \/>}<br \/>getStatus()<br \/>}, [])<br \/>console.log(0, &#8216;formData&#8217;, formData)<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>0 &#8220;formData&#8221; {}<br \/>0 &#8220;formData&#8221; {}<br \/>1 &#8220;formData&#8221; {}<\/p>\n<p>0 &#8220;formData&#8221; {id: 170, buyerId: 10000000424, tenantId: 1, createdBy: &#8220;10000000424&#8221;, createdAt: null,\u00a0\u2026}<\/p>\n<p>2 &#8220;formData&#8221; {}<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\u7b2c\u4e09\u6b21 0 \u65f6 formData \u5df2\u7ecf\u6709\u503c\u4e86.\u4e3a\u4ec0\u4e48\u8fd8\u4f1a\u8d70\u4e00\u6b21 console.log(2) \u5462?      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3537120\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : azcvcza <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e2a\u4eba\u7684\u770b\u6cd5\u662f\uff0chooks \u662f\u63d0\u4f9b\u7ed9\u865a\u62df\u673a\u6267\u884c\u7684\u95ed\u5305\uff0c\u800c\u51fd\u6570\u5f0f\u7ec4\u4ef6\u5c31\u662f\u7ed9\u865a\u62df\u673a\u591a\u6b21\u6267\u884c\u7684\u542b\u6709\u4e0d\u540c\u53c2\u6570\u7684\u51fd\u6570\uff0c\u5f53\u6253\u5370\u4e86 1 \u4e4b\u540e\uff0csetFormData \u89e6\u53d1\u91cd\u65b0\u6e32\u67d3\uff0c\u518d\u8fd0\u884c\u542b\u6709\u65b0\u53c2\u6570\u7684\u51fd\u6570\uff0c\u7136\u540e\u53d1\u73b0 effect \u4e2d\u7684\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u8fc7\u4e86\uff0c\u76f4\u63a5\u8df3\u5230 0\uff0c\u6700\u540e\u6253\u5370\u95ed\u5305\u4e2d\u5269\u4e0b\u6ca1\u6267\u884c\u7684 2 \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3537121\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : onfuns <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             useState \u91cc\u7684 callback \u8c03\u7528\u5176\u5b9e\u662f\u5f02\u6b65\u7684\uff0c\u6240\u4ee5\u4e0d\u80fd\u5728\u8c03\u7528 callback \u540e\u7acb\u9a6c\u83b7\u53d6 console.log(2, &#8216;formData&#8217;, formData)\uff0c\u8fd9\u79cd\u5199\u6cd5\u662f\u9519\u8bef\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3537122\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JimmyChange <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             console.log(1, &#8216;formData&#8217;, formData)<br \/>setFormData(res)<br \/>console.log(2, &#8216;formData&#8217;, formData)<\/p>\n<p>\u8c03\u7528 setFormData \u4f1a\u518d\u6b21\u89e6\u53d1 render\uff0c\u8f93\u51fa 0 formData\uff0c\u4f46\u662f setFormData \u7ed3\u675f\u540e\uff0c\u540e\u8fb9\u7684 console.log \u8fd8\u662f\u4f1a\u6267\u884c\uff0c\u8f93\u51fa 2 &#8220;formData&#8221;                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3537123\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : baxtergu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6309\u7167\u4f60\u7684\u5199\u6cd5\uff1a<br \/>1 \u3001useEffect \u4e2d\u7684\u4ee3\u7801\u53ea\u4f1a\u5728\u7ec4\u4ef6 mount \u4ee5\u540e\u6267\u884c\u4e00\u6b21\uff0c\u4e0d\u7ba1\u4ee5\u540e\u7ec4\u4ef6\u72b6\u6001\u6216\u8005 props \u600e\u4e48\u53d8 useEffect \u4e2d\u7684\u4ee3\u7801\u53ea\u4f1a\u6267\u884c 1 \u6b21\u3002\uff08\u4e5f\u5c31\u662f\u4e3a\u4ec0\u4e48 1 \u548c 2 \u53ea\u4f1a\u8f93\u51fa\u4e00\u6b21\uff0c\u800c 0 \u4f1a\u8f93\u51fa\u591a\u6b21\uff09<br \/>2 \u3001useState \u751f\u6210\u7684 setState \u7684\u6267\u884c\u662f**\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 state \u4e3a\u4e0b\u4e00\u6b21 render \u4f7f\u7528**\u3002\u4f46\u662f useEffect \u8fd9\u6b21\u51fd\u6570\u95ed\u5305\u91cc\u7684 formData \u662f\u4e0a\u4e00\u6b21\u7684 state \u4e5f\u5c31\u662f{}\uff0c\u8fd9\u4e00\u70b9\u8ddf\u7c7b\u7ec4\u4ef6\u7684 this.state \u662f\u5b8c\u5168\u4e0d\u4e00\u6837\u7684\u3002\u7c7b\u7ec4\u4ef6\u7684 this.state \u4fdd\u5b58\u7684\u662f\u72b6\u6001\u5bf9\u8c61\u7684\u4e00\u4e2a\u5f15\u7528\uff0c\u4f46\u662f\u7528 useState \u751f\u6210\u7684 state \u662f\u901a\u8fc7\u95ed\u5305\u4fdd\u5b58\u4e86\u6267\u884c useEffect \u65f6 state \u7684\u5728\u51fd\u6570\u6267\u884c\u4e0a\u4e0b\u6587\u4e2d\u7684\uff0c\u4e5f\u5c31\u662f\u65e7\u7684 state \u5f15\u7528\u3002\u6bcf\u6b21\u91cd\u65b0 render \u4ee5\u540e\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684 state\uff0c\u800c\u4e0d\u662f\u6539\u53d8\u65e7 state \u7684\u5f15\u7528\u91cc\u7684\u503c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>react hooks \u95ee\u9898 \u8cc7\u6df1&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\/162573"}],"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=162573"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/162573\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=162573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=162573"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=162573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}