{"id":200985,"date":"2020-11-24T07:24:48","date_gmt":"2020-11-23T23:24:48","guid":{"rendered":"http:\/\/4563.org\/?p=200985"},"modified":"2020-11-24T07:24:48","modified_gmt":"2020-11-23T23:24:48","slug":"%e5%88%9d%e5%ad%a6%e8%80%85-react-%e5%87%bd%e6%95%b0%e7%bb%84%e4%bb%b6%e5%a6%82%e4%bd%95%e6%8a%8a%e8%87%aa%e5%b7%b1%e7%9a%84-usestate-%e5%90%91%e4%b8%8a%e4%bc%a0%ef%bc%8c%e6%88%96%e8%80%85%e6%98%af","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=200985","title":{"rendered":"\/*\u521d\u5b66\u8005*\/react \u51fd\u6570\u7ec4\u4ef6\u5982\u4f55\u628a\u81ea\u5df1\u7684 useState \u5411\u4e0a\u4f20\uff0c\u6216\u8005\u662f\u5916\u90e8\u5982\u679c\u83b7\u53d6\u67d0\u4e2a\u7ec4\u4ef6\u7684 state"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \/*\u521d\u5b66\u8005*\/react \u51fd\u6570\u7ec4\u4ef6\u5982\u4f55\u628a\u81ea\u5df1\u7684 useState \u5411\u4e0a\u4f20\uff0c\u6216\u8005\u662f\u5916\u90e8\u5982\u679c\u83b7\u53d6\u67d0\u4e2a\u7ec4\u4ef6\u7684 state               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : park4ce666 <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        react \u65b0\u624b\uff0c\u5b66\u7684\u662f\u51fd\u6570\u5f0f\u7ec4\u4ef6\u548c hooks \u7684\u65b9\u5f0f\uff1b<br \/>\u60f3\u505a\u7684\u662f\u7b80\u5355\u7684 &lt;input\/&gt; \u5b50\u7ec4\u4ef6\u4f1a\u6709\u597d\u51e0\u4e2a\uff0c\u5728\u67d0\u4e2a\u9636\u6bb5\u4f1a\u628a\u7528\u6237\u8f93\u5165\u7684\u51e0\u4e2a &lt;input \/&gt;\u91cc\u7684\u503c\u90fd\u6536\u96c6\u8d77\u6765\uff0c\u56e0\u4e3a&lt;input \/&gt;\u662f\u51fd\u6570\u7ec4\u4ef6\u521b\u9020\u7684 \uff0c\u91cc\u9762\u7684\u503c\u662f useState \u4fdd\u5b58\u7684\uff0c\u6211\u8981\u600e\u4e48\u83b7\u53d6\u5230\u6bcf\u4e2a\u5b50\u7ec4\u4ef6\u7684\u503c\uff0c\u8fd8\u662f\u6211\u5e94\u8be5\u7528\u4ec0\u4e48 hook \u53ef\u4ee5\u8ba9\u5b50\u7ec4\u4ef6&lt;input\/&gt;\u81ea\u5df1\u7684\u503c\u548c\u7236\u7ea7\u7ec4\u4ef6\u7ed1\u5b9a\u7684\uff1f\u5e0c\u671b\u5404\u4f4d\u6307\u6761\u8def      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4301703\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leoyzh97 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bf4\u4e0d\u5c31\u662f\u53d7\u63a7\u7ec4\u4ef6\u561b\uff0c\u53ef\u4ee5\u770b\u770b antd form \u8868\u5355\u7684\u5b9e\u73b0\u3002\u5e94\u8be5\u5c31\u662f\u7236\u7ec4\u4ef6\u751f\u6210\u4e00\u4e2a fieldstore \u539f\u6765\u5b58\u50a8\u5b50\u7ec4\u4ef6\u7684\u8868\u5355\u5b57\u6bb5\uff0c\u751f\u6210\u4e00\u4e2a\u6ce8\u518c\u65b9\u6cd5\u7ed9\u5b50\u7ec4\u4ef6\u6ce8\u518c\uff0c\u7136\u540e\u76d1\u542c onchange \u4e8b\u4ef6\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4301704\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leoyzh97 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e0d\u5c31\u662f\u53d7\u63a7\u7ec4\u4ef6\u561b\uff0c\u53ef\u4ee5\u770b\u770b antd form \u8868\u5355\u7684\u5b9e\u73b0\u3002\u5e94\u8be5\u5c31\u662f\u7236\u7ec4\u4ef6\u751f\u6210\u4e00\u4e2a fieldstore \u539f\u6765\u5b58\u50a8\u5b50\u7ec4\u4ef6\u7684\u8868\u5355\u5b57\u6bb5\uff0c\u751f\u6210\u4e00\u4e2a\u6ce8\u518c\u65b9\u6cd5\u7ed9\u5b50\u7ec4\u4ef6\u6ce8\u518c\uff0c\u7136\u540e\u76d1\u542c onchange \u4e8b\u4ef6\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4301705\" 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>                                                             \u4e5f\u6ca1\u5230\u90a3\u4e48\u641e\u4e2d\u95f4\u5c42 form \u7684\u5730\u6b65\uff0c\u4f60\u5c31\u5728 <br \/>`<br \/>const Parent = (props)=&gt;{<br \/> const [pState,setPState] = useState({});<\/p>\n<p> function handlePStateChange(InputName, value){<br \/> setPState({<br \/> &#8230;pState,<br \/> [InputName]:value,<br \/> })<br \/> }<\/p>\n<p> return (<br \/> &lt;div&gt;<br \/> &lt;Child id={&#8216;1&#8217;} handleChange={handlePStateChange} \/&gt;<br \/> &#8230;.<br \/> &lt;Child id={&#8216;n&#8217;} handleChange={handlePStateChange} \/&gt;<br \/> &lt;\/div&gt;<br \/> )<br \/>}<br \/>const Child = (props) =&gt;{<br \/> const [input, setInput] = useState();<\/p>\n<p> return (<br \/> &lt;Input onChange={(e)=&gt;props.handleChange(props.id, e.target.value)} \/&gt;<br \/> )<\/p>\n<p>}<br \/>`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4301706\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : awefeng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7236\u7ec4\u4ef6\u7ed9\u4e2a\u76d1\u542c\u7ed9\u5b50\u7ec4\u4ef6\u5c31\u597d\u4e86 \u56fa\u5b9a\u4e00\u4e0b\u8303\u5f0f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4301707\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : susunus <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             usecallback \u8fd9\u4e2a\u4ece\u7236\u7ec4\u4ef6\u4f20\u7ed9\u81ea\u7ec4\u4ef6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4301708\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ljpCN <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u5b50\u7ec4\u4ef6\u4e0d\u8981\u4fdd\u5b58\u72b6\u6001\uff0c\u72b6\u6001\u63d0\u5347\u5230\u7236\u7ec4\u4ef6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4301709\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : park4ce666 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6536\u5230\u5e76\u9605\u8bfb\u4e86\u5927\u5bb6\u7684\u60f3\u6cd5\u548c\u610f\u89c1\u540e\uff0c\u6211\u6253\u7b97\u8bd5\u4e00\u4e0b\u7ed9\u7236\u7ea7\u7ec4\u4ef6\u521b\u5efa\u4e00\u4e2a use Context \u7684\u94a9\u5b50\uff0c\u7136\u540e\u5b50\u7ec4\u4ef6\u901a\u8fc7 Context \u548c\u7236\u7ea7\u5171\u4eab\u72b6\u6001\uff0c\u8fd9\u4e48\u5199\uff0c\u6bcf\u4e00\u6b21\u5e94\u7528\u7ec4\u4ef6\u7684\u65f6\u5019\u53ef\u80fd\u8981\u6839\u636e\u5b50\u7ec4\u4ef6\u7684\u6570\u91cf\u548c\u5bf9\u5e94\u7684\u5b57\u6bb5\u5c5e\u6027\u5728\u7236\u7ea7\u624b\u52a8\u5148\u9884\u8bbe\u597d\uff0c\u611f\u89c9\u8fd8\u662f\u6709\u4e9b\u6b7b\u677f\uff0c\u76ee\u524d\u6211\u7684\u8111\u5b50\u53ea\u591f\u5199\u5230\u8fd9\u4e00\u6b65\u4e86\uff0c\u4e0d\u77e5\u9053\u8fd8\u80fd\u5426\u4f18\u5316\u5230\u81ea\u52a8\u9002\u914d\u5b50\u7ec4\u4ef6\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4301710\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : KuroNekoFan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9700\u8981\u5bfc\u51fa\u72b6\u6001\u53d8\u5316\u7684\u7ec4\u4ef6\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a props, onCompStateChange\uff0c\u8c03\u7528\u65b9 \/\u7236\u7ec4\u4ef6\u901a\u8fc7\u4f20\u5165\u4e00\u4e2a\u51fd\u6570\u5c31\u53ef\u4ee5\u62ff\u5230\u5176\u72b6\u6001\u53d8\u5316\u4e86<br \/>\u5b50\u7ec4\u4ef6\u4e0d\u4fdd\u5b58\u72b6\u6001\u4e0d\u662f\u4e00\u4e2a\u597d\u505a\u6cd5\uff0c\u8be5\u6709\u672c\u5730\u72b6\u6001\u5c31\u5e94\u8be5\u6709\u672c\u5730\u72b6\u6001                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4301711\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : justsosososo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             hook \u53ea\u662f\u7528\u6765\u7b80\u5199\u7684\u65b0\u7279\u6027 \u8be5 props \u8fd8\u662f\u5f97 props \u4e0d\u8981\u88ab\u8fd9\u4e9b\u6982\u5ff5\u6df7\u6dc6\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\/*\u521d\u5b66\u8005*\/react \u51fd\u6570\u7ec4\u4ef6&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\/200985"}],"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=200985"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/200985\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200985"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}