{"id":5597,"date":"2022-03-21T00:24:12","date_gmt":"2022-03-20T16:24:12","guid":{"rendered":"http:\/\/39.106.39.94\/?p=5597"},"modified":"2022-04-20T16:19:13","modified_gmt":"2022-04-20T08:19:13","slug":"props%e5%92%8cstate","status":"publish","type":"post","link":"https:\/\/lilichao.com\/?p=5597","title":{"rendered":"state"},"content":{"rendered":"\n<p>props\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u90fd\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u4f7f\u5f97React\u7ec4\u4ef6\u4e0d\u80fd\u968f\u7740props\u7684\u6539\u53d8\u800c\u6539\u53d8\u3002\u4f46\u5728\u5b9e\u9645\u7684\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u66f4\u5e0c\u671b\u7684\u662f\u6570\u636e\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u9875\u9762\u4e5f\u4f1a\u968f\u7740\u6570\u636e\u4e00\u8d77\u53d8\u5316\u3002React\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86state\u7528\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n<p>state\u548cprops\u7c7b\u4f3c\uff0c\u90fd\u662f\u4e00\u79cd\u5b58\u50a8\u5c5e\u6027\u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u4e0d\u540c\u70b9\u5728\u4e8estate\u53ea\u5c5e\u4e8e\u5f53\u524d\u7ec4\u4ef6\uff0c\u5176\u4ed6\u7ec4\u4ef6\u65e0\u6cd5\u8bbf\u95ee\u3002\u5e76\u4e14state\u662f\u53ef\u53d8\u7684\uff0c\u5f53\u5176\u53d1\u751f\u53d8\u5316\u540e\u7ec4\u4ef6\u4f1a\u81ea\u52a8\u91cd\u65b0\u6e32\u67d3\uff0c\u4ee5\u4f7f\u53d8\u5316\u5728\u9875\u9762\u4e2d\u5448\u73b0\u3002<\/p>\n\n\n\n<p>\u73b0\u6709\u5982\u4e0b\u7ec4\u4ef6Clock\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React from 'react';\n\u200b\nconst Clock = () =&gt; {\n\u200b\n &nbsp;  let date = new Date().toLocaleTimeString();\n\u200b\n &nbsp;  const clickHandler = ()=&gt;{\n\u200b\n &nbsp;  };\n\u200b\n &nbsp; &nbsp;return (\n &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {date}\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;button onClick={clickHandler}&gt;\u5237\u65b0&lt;\/button&gt;\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;\n &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;\n &nbsp;  );\n};\n\u200b\nexport default Clock;\n\u200b<\/pre>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u5e0c\u671b\u70b9\u51fb\u6309\u94ae\u4ee5\u540e\uff0c\u65f6\u95f4\u53ef\u4ee5\u5237\u65b0\u76f4\u63a5\u663e\u793a\u4e00\u4e2a\u5f53\u524d\u7684\u6700\u65b0\u65e5\u671f\u3002\u5982\u679c\u76f4\u63a5\u5728clickHandler\u4e2d\u4fee\u6539date\u7684\u503c\u662f\u65e0\u6548\u7684\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/\u7701\u7565\n\u200b\nconst clickHandler = ()=&gt;{\n    date = new Date().toLocaleTimeString();\n    console.log(date);\n};\n\u200b\n\/\/\u7701\u7565<\/pre>\n\n\n\n<p>state\u4e5f\u53ef\u4ee5\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f46\u662f\u5b83\u7684\u5b9a\u4e49\u65b9\u5f0f\u4e0d\u592a\u4e00\u6837\uff0c\u6211\u4eec\u4ee5\u51fd\u6570\u7ec4\u4ef6\u4e3a\u4f8b\u6765\u4ecb\u7ecdstate\u7684\u4f7f\u7528\u65b9\u5f0f\uff08\u7c7b\u7ec4\u4ef6\u54b1\u4eec\u540e\u8fb9\u518d\u8bf4\uff09\u3002\u5728\u51fd\u6570\u4e2d\u4f7f\u7528state\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u79cd\u94a9\u5b50\uff08hook\uff09\u51fd\u6570\u3002\u94a9\u5b50\u51fd\u6570\u53ef\u4ee5\u5728\u51fd\u6570\u7ec4\u4ef6\u4e2d\u201c\u52fe\u51fa\u201dReact\u7684\u7279\u6027\uff0c\u6362\u53e5\u8bdd\u8bf4\u6211\u4eec\u8981\u7528\u4e00\u4e2a\u51fd\u6570\u201c\u52fe\u51fa\u201dstate\u3002<\/p>\n\n\n\n<p>\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const [state, setState] = useState(initialState);<\/pre>\n\n\n\n<p>\u901a\u8fc7\u94a9\u5b50\u51fd\u6570useState()\u52fe\u51fastate\uff0cuseState()\u4e2d\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u521d\u59cb\u503c\uff0c\u8fd9\u4e2a\u503c\u5c31\u662f\u4f60\u5e0c\u671b\u5728\u53d8\u91cf\u4e2d\u5b58\u50a8\u7684\u503c\u3002\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u6709\u4e24\u4e2a\u5143\u7d20\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u5b58\u50a8\u4e86\u503c\u7684\u53d8\u91cf\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f\u4e00\u4e2a\u51fd\u6570\u7528\u6765\u5bf9\u503c\u8fdb\u884c\u4fee\u6539\u3002\u6bd4\u5982\u4e0a\u8fb9\u7684\u6848\u4f8b\uff0c\u53ef\u4ee5\u8fd9\u6837\u4fee\u6539\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React, {useState} from 'react';<br>\u200b<br>const Clock = () =&gt; {<br>\u200b<br> &nbsp; &nbsp;const [date, setDate] = useState(new Date().toLocaleTimeString());<br>\u200b<br> &nbsp; &nbsp;const clickHandler = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;setDate(new Date().toLocaleTimeString());<br> &nbsp;  };<br>\u200b<br> &nbsp; &nbsp;return (<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {date}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;button onClick={clickHandler}&gt;\u5237\u65b0&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp;  );<br>};<br>\u200b<br>export default Clock;<\/pre>\n\n\n\n<p>\u4f7f\u7528useState()\u201c\u52fe\u51fa\u201d\u7684\u53d8\u91cf\u5c31\u662f\u4e00\u4e2a\u666e\u901a\u53d8\u91cf\uff0c\u5b83\u91cc\u8fb9\u5b58\u50a8\u4e86\u521d\u59cb\u5316\u7684\u503c\uff0c\u8fd9\u4e2a\u53d8\u91cf\u548c\u5176\u4ed6\u53d8\u91cf\u6ca1\u4ec0\u4e48\u5927\u533a\u522b\uff0c\u540c\u6837\u4fee\u6539\u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\u4e5f\u4e0d\u4f1a\u5bf9\u7ec4\u4ef6\u4ea7\u751f\u5b9e\u8d28\u6027\u7684\u5f71\u54cd\uff0c\u6240\u4ee5\u4e0d\u8981\u5c1d\u8bd5\u76f4\u63a5\u4e3astate\u8d4b\u503c\u3002useState()\u201c\u52fe\u51fa\u201d\u7684\u51fd\u6570\u7528\u6765\u4fee\u6539state\u7684\u503c\uff0c\u4ed6\u9700\u8981\u4e00\u4e2a\u65b0\u7684state\u503c\u4f5c\u4e3a\u53c2\u6570\uff0c\u8c03\u7528\u540e\u4f1a\u89e6\u53d1\u7ec4\u4ef6\u7684\u91cd\u65b0\u6e32\u67d3\uff0c\u4ece\u800c\u4f7f\u5f97\u9875\u9762\u5237\u65b0\uff0c\u5728\u6bcf\u6b21\u7684\u91cd\u65b0\u6e32\u67d3\u4e2d\u90fd\u4f1a\u4f7f\u7528\u65b0\u7684state\u503c\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>props\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u90fd\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u4f7f\u5f97React\u7ec4\u4ef6\u4e0d\u80fd\u968f\u7740props\u7684\u6539\u53d8\u800c\u6539\u53d8\u3002\u4f46\u5728\u5b9e\u9645\u7684\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5737,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198],"tags":[200,202,199,201,203],"_links":{"self":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5597"}],"collection":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5597"}],"version-history":[{"count":5,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5597\/revisions"}],"predecessor-version":[{"id":5760,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5597\/revisions\/5760"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/media\/5737"}],"wp:attachment":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}