{"id":206,"date":"2021-02-10T10:19:06","date_gmt":"2021-02-10T02:19:06","guid":{"rendered":"http:\/\/gjweb.top\/?p=206"},"modified":"2021-02-10T10:19:07","modified_gmt":"2021-02-10T02:19:07","slug":"hooks%e7%bb%84%e4%bb%b6%e8%af%a6%e8%a7%a3%e4%b8%8b","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=206","title":{"rendered":"hooks\u7ec4\u4ef6\u8be6\u89e3(\u4e0b)"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>useRef<\/li><li>forwardRef<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">1. useRef<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>useRef<ul><li>\u7528\u4e8e\u5728\u4e00\u4e2a\u7ec4\u4ef6\u4e0d\u65adrender\u4fdd\u6301\u4e0d\u53d8\u7684\u503c<\/li><li>\u521d\u59cb\u5316: const count = useRef(0)<\/li><li>\u8bfb\u53d6: count.current<\/li><\/ul><\/li><li>useRef\u5728\u53d8\u5316\u662f\u4e0d\u4f1a\u81ea\u52a8render<ul><li>\u5982\u679c\u60f3\u8981\u5f53\u53d8\u5316\u91cd\u65b0\u6e32\u67d3UI,\u9700\u8981\u5982\u4e0b\u5199\u6cd5<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;function App(){<br> &nbsp; &nbsp; &nbsp; &nbsp;const count = useRef(0)<br> &nbsp; &nbsp; &nbsp; &nbsp;const [n,setN] = useState(0)<br> &nbsp; &nbsp; &nbsp; &nbsp;const [_,set_] = useState(0)<br> &nbsp; &nbsp; &nbsp; &nbsp;const addN =()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count.current+=1<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;set_(Math.random())<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; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ref:{count.current}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;button onClick={addN}&gt;addN&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  )<br>\u200b<br> &nbsp;  }<br>\u200b<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. forwardRef<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>useRef\u7684props\u65e0\u6cd5\u4f20\u9012ref\u5c5e\u6027<\/li><li>forwarRef\u53ef\u4ee5\u5b9e\u73b0ref\u7684\u4f20\u9012<\/li><li>\u4e24\u6b21ref\u4f20\u9012\u5f97\u5230button\u7684\u5f15\u7528<\/li><li>\u7528\u6cd5<ul><li>\u53ef\u4ee5\u7528\u6765\u5f15\u7528dom\u5bf9\u8c61<\/li><li>\u4e5f\u53ef\u4ee5\u7528\u6765\u5f15\u7528\u666e\u901a\u5bf9\u8c61 \u57fa\u672c\u4f7f\u7528<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;function App(){<br> &nbsp; &nbsp; &nbsp; &nbsp;const buttonRef = useRef(null)<br> &nbsp; &nbsp; &nbsp; &nbsp;return (<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;Button3 ref={buttonRef}&gt;button&lt;\/Button3&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;const Button3 = React.forwardRef((props,ref)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;return(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;button ref={ref} {...props}&gt;&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  })<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>useRef forwardRef 1. useRef useRef \u7528\u4e8e\u5728\u4e00\u4e2a\u7ec4\u4ef6\u4e0d\u65adrender\u4fdd\u6301\u4e0d\u53d8\u7684 [&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-206","post","type-post","status-publish","format-standard","hentry","category-react"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/206","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=206"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/206\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}