{"id":4678,"date":"2023-01-04T17:30:55","date_gmt":"2023-01-04T09:30:55","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4678"},"modified":"2023-01-04T17:30:55","modified_gmt":"2023-01-04T09:30:55","slug":"%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e3%80%8c%e4%b8%8d%e5%8f%97%e6%8e%a7%e5%88%b6%e7%9a%84%e8%bc%b8%e5%85%a5%e5%85%83%e7%b4%a0%e8%bd%89%e6%8f%9b%e3%80%8d%e7%9a%84%e9%8c%af%e8%aa%a4","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e3%80%8c%e4%b8%8d%e5%8f%97%e6%8e%a7%e5%88%b6%e7%9a%84%e8%bc%b8%e5%85%a5%e5%85%83%e7%b4%a0%e8%bd%89%e6%8f%9b%e3%80%8d%e7%9a%84%e9%8c%af%e8%aa%a4\/","title":{"rendered":"\u89e3\u6c7aReact.js\u4e2d\u300c\u4e0d\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\u8f49\u63db\u300d\u7684\u932f\u8aa4"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.js, Error, uncontrolled input, controlled input, component, lifetime\"><\/p>\n<h1>\u89e3\u6c7a React.js \u4e2d\u7684\u300cError: A component is changing an uncontrolled input of type text to be controlled\u300d\u932f\u8aa4<\/h1>\n<p>React.js \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u7aef\u61c9\u7528\u7a0b\u5e8f\u7684 JavaScript \u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u69cb\u5efa\u51fa\u9ad8\u54c1\u8cea\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u7136\u800c\uff0c\u5728\u958b\u767c React.js \u61c9\u7528\u7a0b\u5e8f\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u932f\u8aa4\uff0c\u5176\u4e2d\u4e00\u500b\u662f\u300cError: A component is changing an uncontrolled input of type text to be controlled\u300d\u3002<\/p>\n<p>\u9019\u500b\u932f\u8aa4\u7684\u610f\u601d\u662f\uff0c\u4e00\u500b\u7d44\u4ef6\u6b63\u5728\u6539\u8b8a\u4e00\u500b\u672a\u53d7\u63a7\u5236\u7684\u6587\u672c\u8f38\u5165\uff0c\u4ee5\u8b8a\u6210\u53d7\u63a7\u5236\u7684\u8f38\u5165\u3002\u8f38\u5165\u5143\u7d20\u4e0d\u61c9\u8a72\u5f9e\u672a\u53d7\u63a7\u5236\u5230\u53d7\u63a7\u5236\uff08\u6216\u53cd\u4e4b\u4ea6\u7136\uff09\u3002\u56e0\u6b64\uff0c\u958b\u767c\u8005\u5fc5\u9808\u6c7a\u5b9a\u5728\u7d44\u4ef6\u7684\u751f\u547d\u9031\u671f\u4e2d\u4f7f\u7528\u53d7\u63a7\u5236\u6216\u672a\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u53d7\u63a7\u5236\u548c\u672a\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\uff1f<\/h2>\n<p>\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\u662f\u6307\uff0c\u8f38\u5165\u5143\u7d20\u7684\u503c\u662f\u7531 React.js \u63a7\u5236\u7684\uff0c\u800c\u672a\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\u5247\u662f\u6307\uff0c\u8f38\u5165\u5143\u7d20\u7684\u503c\u662f\u7531\u4f7f\u7528\u8005\u63a7\u5236\u7684\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u4f60\u6b63\u5728\u958b\u767c\u4e00\u500b\u8868\u55ae\uff0c\u5176\u4e2d\u6709\u4e00\u500b\u6587\u672c\u8f38\u5165\u6846\uff0c\u7528\u65bc\u8f38\u5165\u7528\u6236\u7684\u540d\u5b57\u3002\u5982\u679c\u4f60\u4f7f\u7528\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\uff0c\u5247 React.js \u6703\u63a7\u5236\u8f38\u5165\u6846\u4e2d\u7684\u503c\uff0c\u800c\u4f7f\u7528\u8005\u7121\u6cd5\u66f4\u6539\u8f38\u5165\u6846\u4e2d\u7684\u503c\u3002\u76f8\u53cd\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u672a\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\uff0c\u5247\u4f7f\u7528\u8005\u53ef\u4ee5\u81ea\u7531\u66f4\u6539\u8f38\u5165\u6846\u4e2d\u7684\u503c\u3002<\/p>\n<h2>\u70ba\u4ec0\u9ebc\u4e0d\u80fd\u5f9e\u672a\u53d7\u63a7\u5236\u5230\u53d7\u63a7\u5236\uff1f<\/h2>\n<p>React.js \u662f\u4e00\u500b\u57fa\u65bc\u7d44\u4ef6\u7684\u6846\u67b6\uff0c\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\u7d44\u4ef6\u7684\u53ef\u91cd\u7528\u6027\u3002\u56e0\u6b64\uff0cReact.js \u5c0d\u65bc\u7d44\u4ef6\u7684\u8f38\u5165\u5143\u7d20\u7684\u8b8a\u5316\u6709\u4e00\u5b9a\u7684\u8981\u6c42\uff0c\u4ee5\u78ba\u4fdd\u7d44\u4ef6\u7684\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<p>\u5982\u679c\u4e00\u500b\u7d44\u4ef6\u7684\u8f38\u5165\u5143\u7d20\u5f9e\u672a\u53d7\u63a7\u5236\u5230\u53d7\u63a7\u5236\uff0c\u5247\u53ef\u80fd\u6703\u5c0e\u81f4\u7d44\u4ef6\u7684\u884c\u70ba\u4e0d\u4e00\u81f4\uff0c\u751a\u81f3\u53ef\u80fd\u5c0e\u81f4\u7d44\u4ef6\u51fa\u73fe\u56b4\u91cd\u7684\u932f\u8aa4\u3002\u56e0\u6b64\uff0cReact.js \u5c0d\u65bc\u7d44\u4ef6\u7684\u8f38\u5165\u5143\u7d20\u7684\u8b8a\u5316\u6709\u4e00\u5b9a\u7684\u8981\u6c42\uff0c\u4ee5\u78ba\u4fdd\u7d44\u4ef6\u7684\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<h2>\u5982\u4f55\u89e3\u6c7a\u300cError: A component is changing an uncontrolled input of type text to be controlled\u300d\u932f\u8aa4\uff1f<\/h2>\n<p>\u8981\u89e3\u6c7a\u300cError: A component is changing an uncontrolled input of type text to be controlled\u300d\u932f\u8aa4\uff0c\u958b\u767c\u8005\u5fc5\u9808\u6c7a\u5b9a\u5728\u7d44\u4ef6\u7684\u751f\u547d\u9031\u671f\u4e2d\u4f7f\u7528\u53d7\u63a7\u5236\u6216\u672a\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u5728\u7d44\u4ef6\u7684\u751f\u547d\u9031\u671f\u4e2d\u4f7f\u7528\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\uff0c\u5247\u4f60\u53ef\u4ee5\u4f7f\u7528 React.js \u7684 <code>setState()<\/code> \u65b9\u6cd5\u4f86\u66f4\u65b0\u8f38\u5165\u5143\u7d20\u7684\u503c\uff0c\u4ee5\u78ba\u4fdd\u8f38\u5165\u5143\u7d20\u7684\u503c\u662f\u7531 React.js \u63a7\u5236\u7684\u3002<\/p>\n<pre class=\"brush: javascript\">\n\/\/ \u4f7f\u7528 setState() \u65b9\u6cd5\u66f4\u65b0\u8f38\u5165\u5143\u7d20\u7684\u503c\nthis.setState({\n  inputValue: 'new value'\n});\n<\/pre>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u5728\u7d44\u4ef6\u7684\u751f\u547d\u9031\u671f\u4e2d\u4f7f\u7528\u672a\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\uff0c\u5247\u4f60\u53ef\u4ee5\u4f7f\u7528 React.js \u7684 <code>defaultValue<\/code> \u5c6c\u6027\u4f86\u8a2d\u7f6e\u8f38\u5165\u5143\u7d20\u7684\u9ed8\u8a8d\u503c\uff0c\u4ee5\u78ba\u4fdd\u8f38\u5165\u5143\u7d20\u7684\u503c\u662f\u7531\u4f7f\u7528\u8005\u63a7\u5236\u7684\u3002<\/p>\n<pre class=\"brush: javascript\">\n\/\/ \u4f7f\u7528 defaultValue \u5c6c\u6027\u8a2d\u7f6e\u8f38\u5165\u5143\u7d20\u7684\u9ed8\u8a8d\u503c\n<input type=\"text\" defaultValue=\"default value\" \/>\n<\/pre>\n<p>\u7e3d\u4e4b\uff0c\u8981\u89e3\u6c7a\u300cError: A component is changing an uncontrolled input of type text to be controlled\u300d\u932f\u8aa4\uff0c\u958b\u767c\u8005\u5fc5\u9808\u6c7a\u5b9a\u5728\u7d44\u4ef6\u7684\u751f\u547d\u9031\u671f\u4e2d\u4f7f\u7528\u53d7\u63a7\u5236\u6216\u672a\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\uff0c\u4ee5\u78ba\u4fdd\u7d44\u4ef6\u7684\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u8a0e\u8ad6\u4e86 React.js \u4e2d\u7684\u300cError: A component is changing an uncontrolled input of type text to be controlled\u300d\u932f\u8aa4\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u6c7a\u8a72\u932f\u8aa4\u7684\u65b9\u6cd5\u3002\u6211\u5011\u77e5\u9053\uff0c\u8981\u89e3\u6c7a\u8a72\u932f\u8aa4\uff0c\u958b\u767c\u8005\u5fc5\u9808\u6c7a\u5b9a\u5728\u7d44\u4ef6\u7684\u751f\u547d\u9031\u671f\u4e2d\u4f7f\u7528\u53d7\u63a7\u5236\u6216\u672a\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\uff0c\u4ee5\u78ba\u4fdd\u7d44\u4ef6\u7684\u53ef\u91cd\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aReact.js\u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u7aef\u61c9\u7528\u7a0b\u5e8f\u7684JavaScript\u5eab\uff0c\u4f46\u5b83\u4e5f\u6703\u51fa\u73fe\u300c\u4e0d\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\u8f49\u63db\u300d\u7684\u932f\u8aa4\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u89e3\u6c7aReact.js\u4e2d\u7684\u300c\u4e0d\u53d7\u63a7\u5236\u7684\u8f38\u5165\u5143\u7d20\u8f49\u63db\u300d\u932f\u8aa4\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u4f7f\u7528React.js\u6642\u907f\u514d\u51fa\u73fe\u6b64\u985e\u932f\u8aa4\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-4678","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1ds","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4678"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4678\/revisions"}],"predecessor-version":[{"id":4679,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4678\/revisions\/4679"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}