{"id":208,"date":"2021-02-10T10:19:39","date_gmt":"2021-02-10T02:19:39","guid":{"rendered":"http:\/\/gjweb.top\/?p=208"},"modified":"2021-02-10T10:19:40","modified_gmt":"2021-02-10T02:19:40","slug":"userefusecontent%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=208","title":{"rendered":"useRef&amp;useContent\u7684\u7528\u6cd5"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>useRef<\/li><li>useContent<\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">1. useRef\u7684\u7528\u6cd5<\/h1>\n\n\n\n<p>\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8eDIV\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;function App(){<br> &nbsp; &nbsp; &nbsp; &nbsp;const nRef = React.useRef(0)<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;p&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {nRef.current}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u6b64\u65f6\u7684nrf\u662f\u4e0d\u4f1a\u66f4\u65b0\u5230\u89c6\u56fe\u7684<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;button onClick={()=&gt;({nRef.current+1})}&gt;add&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/p&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }<\/pre>\n\n\n\n<p>\u5f3a\u5236\u66f4\u65b0(\u63a8\u8350)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;function App(){<br> &nbsp; &nbsp; &nbsp; &nbsp;const nRef = React.useRef(0)<br> &nbsp; &nbsp; &nbsp; &nbsp;const update = React.useEffect()[1]<br> &nbsp; &nbsp; &nbsp; &nbsp;const add = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return ((nRef.current++),update(nRef.current++))<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;p&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {nRef.current}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u6b64\u65f6\u7684nrf\u662f\u4e0d\u4f1a\u66f4\u65b0\u5230\u89c6\u56fe\u7684<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;button onClick={add}&gt;add&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/p&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">1. useContent\u7684\u7528\u6cd5<\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;<br>\u200b<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>useRef useContent 1. useRef\u7684\u7528\u6cd5 \u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8eDIV\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u6570\u636e &#038;nbsp [&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-208","post","type-post","status-publish","format-standard","hentry","category-react"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/208","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=208"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}