{"id":210,"date":"2021-02-10T10:20:15","date_gmt":"2021-02-10T02:20:15","guid":{"rendered":"http:\/\/gjweb.top\/?p=210"},"modified":"2021-09-09T09:04:52","modified_gmt":"2021-09-09T01:04:52","slug":"4-%e5%87%bd%e6%95%b0%e7%bb%84%e4%bb%b6%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=210","title":{"rendered":"4. \u51fd\u6570\u7ec4\u4ef6\u8be6\u89e3"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>\u521b\u5efa\u65b9\u5f0f<\/li><li>\u51fd\u6570\u7ec4\u4ef6\u7684state<\/li><li>\u51fd\u6570\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u521b\u5efa\u65b9\u5f0f<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;import React from \"react\"<br> &nbsp; &nbsp;\/\/\u7bad\u5934\u51fd\u6570\u521b\u5efa<br> &nbsp; &nbsp;const Hello = (props)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;return (<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;{props.name}&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  };<br> &nbsp; &nbsp;\/\/ \u7b80\u5199<br> &nbsp; &nbsp;const Hello = props =&gt; &lt;div&gt;{props.name}&lt;\/div&gt;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/\u666e\u901a\u51fd\u6570\u521b\u5efa<br> &nbsp; &nbsp;function Hello (props){<br> &nbsp; &nbsp; &nbsp; &nbsp;return &lt;div&gt;{props.name}&lt;\/div&gt;<br> &nbsp;  }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u51fd\u6570\u7ec4\u4ef6\u7684state<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u51fd\u6570\u7ec4\u4ef6\u4e2d\u6ca1\u6709state,\u6ca1\u6709\u751f\u547d\u5468\u671f<ul><li>REact V16.8 \u89e3\u51b3\u65b9\u6848 Hooks API \u7684 useSatte \u53ef\u4ee5\u89e3\u51b3<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;const [n.setN] = React.useState(0);<br> &nbsp; &nbsp;setN(n+1)<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2dsetN\u6c38\u8fdc\u4e0d\u4f1a\u66f4\u65b0n,\u4ed6\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684n<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u51fd\u6570\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u751f\u547d\u4e2d\u671f\u7528 Hooks API\u7684useEffect \u53ef\u4ee5\u89e3\u51b3<ul><li>\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u526f\u4f5c\u7528\u95ee\u9898 -\u53ea\u5728\u7b2c\u4e00\u6b21\u6e32\u67d3\u7684\u65f6\u5019\u8c03\u7528<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"> \u00a0 \u00a0import React,{useState} from \"react\"\n \u00a0 \u00a0\/\/\u7bad\u5934\u51fd\u6570\u521b\u5efa\n \u00a0 \u00a0const Hello = (props)=>{\n \u00a0 \u00a0 \u00a0 \u00a0const [n,setN] = useState(0);\n \u00a0 \u00a0 \u00a0 \u00a0const onClick=()=>{setN(n+1)};\n \u00a0 \u00a0 \u00a0 \u00a0\n \u00a0 \u00a0 \u00a0 \u00a0\/\/\u9996\u6b21\u6e32\u67d3\u6267\u884c\u4e00\u6b21\n \u00a0 \u00a0 \u00a0 \u00a0useEffect(()=>{\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0console.log(\"\u6e32\u67d3\u4e86\")\n \u00a0 \u00a0 \u00a0  },[]);\n \u00a0 \u00a0 \u00a0 \u00a0\n \u00a0 \u00a0 \u00a0 \u00a0\/\/n\u53d8\u5316\u4e86\u5c31\u6267\u884c\n \u00a0 \u00a0 \u00a0 \u00a0useEffect(()=>{\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0console.log(\"n update\")\n \u00a0 \u00a0 \u00a0  },[n]);\n \u00a0 \u00a0 \u00a0 \u00a0\n \u00a0 \u00a0 \u00a0 \u00a0\/\/\u9500\u6bc1\u7684\u65f6\u5019\u6267\u884c\n \u00a0 \u00a0 \u00a0 \u00a0useEffect(()=>{\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return ()=>{\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0console.log(\"\u8981\u9500\u6bc1\u4e86\")\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  }\n \u00a0 \u00a0 \u00a0  })\n \u00a0 \u00a0 \u00a0 \u00a0\n \u00a0 \u00a0 \u00a0 \u00a0\n \u00a0 \u00a0 \u00a0 \u00a0return (\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;div>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  {n}\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;button onClick={onClick}>add&lt;\/button>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;\/div>\n \u00a0 \u00a0 \u00a0  )\n \u00a0  };\n\u200b<\/pre>\n\n\n\n<p>\u6a21\u4effcomponentDidUpdate\u4e2d\u7684\u95ee\u9898<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> \u00a0 \u00a0function App (){\n \u00a0 \u00a0 \u00a0 \u00a0const [n,setN] = useState(0)\n \u00a0 \u00a0 \u00a0 \u00a0const add = ()=>{\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0setN(n+1)\n \u00a0 \u00a0 \u00a0  };\n \u00a0 \u00a0 \u00a0 \u00a0useEffect(()=>{\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0console.log(\"n\u7684\u503c\u53d8\u5316\u4e86\")\n \u00a0 \u00a0 \u00a0  },[n])\n \u00a0 \u00a0 \u00a0 \u00a0return (\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;div>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  {n}\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;button onClick={this.add}>add&lt;\/button>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;\/div>\n \u00a0 \u00a0 \u00a0  )\n \u00a0  }<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u76d1\u542c\u5728\u9875\u9762\u521d\u59cb\u5316\u7684\u65f6\u5019\u4e5f\u4f1a\u88ab\u76d1\u542c\u5230,\u539f\u7406\u662fn\u6709\u521d\u59cb\u7684undefind\u53d8\u6210\u4e860;\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u9700\u8981\u91cd\u65b0\u81ea\u5b9a\u4e49\u4e00\u4e0buseEffect;\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;import React,{useState,useEffect} from \"react\"<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/\u81ea\u5b9a\u4e49useEffect\u5b9e\u73b0componentDidUpdate<br> &nbsp; &nbsp;const useUpdate = (fn,dep)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;const [const,setCount] = useState(0);<br> &nbsp; &nbsp; &nbsp; &nbsp;useEffect(()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setCouent((x)=&gt;{x+1})<br> &nbsp; &nbsp; &nbsp;  },[dep])<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;useEffect(()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(const &gt;1){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fn()<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  },[count,fn])<br> &nbsp;  }<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;function App(){<br> &nbsp; &nbsp; &nbsp; &nbsp;const [n,setN] = useState(0)<br> &nbsp; &nbsp; &nbsp; &nbsp;useUpdate(()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(\"n \u53d1\u751f\u4e86\u53d8\u5316\")<br> &nbsp; &nbsp; &nbsp;  },n)<br> &nbsp; &nbsp; &nbsp; &nbsp;add = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setN(x+1)<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;return(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {n}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;button onClick={this.add}&gt;add&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }<br>\u200b<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u521b\u5efa\u65b9\u5f0f \u51fd\u6570\u7ec4\u4ef6\u7684state \u51fd\u6570\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f 1. \u521b\u5efa\u65b9\u5f0f &nbsp; &nbsp;import R [&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-210","post","type-post","status-publish","format-standard","hentry","category-react"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/210","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=210"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}