{"id":4727,"date":"2025-05-31T13:53:23","date_gmt":"2025-06-04T06:12:22","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4727"},"modified":"2025-06-04T13:53:23","modified_gmt":"2025-06-04T06:12:22","slug":"%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e3%80%8cerroracomponentischanginganuncontrolledinputoftypetexttobecontrolled%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%8cerroracomponentischanginganuncontrolledinputoftypetexttobecontrolled%e3%80%8d%e7%9a%84%e9%8c%af%e8%aa%a4\/","title":{"rendered":"\u89e3\u6c7a React.js \u4e2d\u7684\u300c\u672a\u63a7\u5236\u8f38\u5165\u8f49\u70ba\u53d7\u63a7\u5236\u8f38\u5165\u300d\u932f\u8aa4\uff1a\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.js, \u8f38\u5165\u932f\u8aa4, \u53d7\u63a7\u5236\u8f38\u5165, \u672a\u63a7\u5236\u8f38\u5165, JavaScript, \u524d\u7aef\u958b\u767c\"><\/p>\n<h1>\u89e3\u6c7a React.js \u4e2d\u7684\u300c\u672a\u63a7\u5236\u8f38\u5165\u8f49\u70ba\u53d7\u63a7\u5236\u8f38\u5165\u300d\u932f\u8aa4\uff1a\u5b8c\u6574\u6307\u5357<\/h1>\n<p>\u5728\u73fe\u4ee3\u524d\u7aef\u958b\u767c\u4e2d\uff0cReact.js \u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684 JavaScript \u5eab\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u69cb\u5efa\u9ad8\u6548\u7684\u7528\u6236\u7aef\u61c9\u7528\u7a0b\u5e8f\u3002\u7136\u800c\uff0c\u7576\u4f60\u5728\u958b\u767c\u904e\u7a0b\u4e2d\u9047\u5230\u932f\u8aa4\u4fe1\u606f\uff1a\u201cError: A component is changing an uncontrolled input of type text to be controlled\u201d\u6642\uff0c\u9019\u53ef\u80fd\u6703\u8b93\u4f60\u611f\u5230\u56f0\u60d1\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u9019\u500b\u932f\u8aa4\u7684\u4f86\u6e90\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\uff0c\u4e26\u5e6b\u52a9\u4f60\u8f15\u9b06\u89e3\u6c7a\u9019\u4e00\u5e38\u898b\u554f\u984c\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u300c\u672a\u63a7\u5236\u8f38\u5165\u8f49\u70ba\u53d7\u63a7\u5236\u8f38\u5165\u300d\u932f\u8aa4\uff1f<\/h2>\n<p>\u9019\u500b\u932f\u8aa4\u901a\u5e38\u51fa\u73fe\u5728\u4f60\u5617\u8a66\u5c07\u4e00\u500b\u672a\u53d7\u63a7\u5236\u7684\u6587\u672c\u8f38\u5165\u8b8a\u6210\u53d7\u63a7\u5236\u7684\u8f38\u5165\u6642\u3002\u672a\u63a7\u5236\u8f38\u5165\u662f\u6307\u5176\u503c\u4e0d\u7531 React \u7684 state \u63a7\u5236\uff0c\u800c\u53d7\u63a7\u5236\u8f38\u5165\u5247\u662f\u503c\u7531 state \u4f86\u7ba1\u7406\u3002\u7576\u4f60\u5728\u8868\u55ae\u4e2d\u6df7\u5408\u4f7f\u7528\u9019\u5169\u7a2e\u6a21\u5f0f\u6642\uff0cReact \u5c31\u6703\u62cb\u51fa\u9019\u500b\u932f\u8aa4\u3002<\/p>\n<h2>\u5982\u4f55\u89e3\u6c7a\u6b64\u932f\u8aa4\uff1f<\/h2>\n<p>\u70ba\u4e86\u6392\u9664\u300c\u672a\u63a7\u5236\u8f38\u5165\u8f49\u70ba\u53d7\u63a7\u5236\u8f38\u5165\u300d\u7684\u932f\u8aa4\uff0c\u4f60\u9700\u8981\u78ba\u4fdd\u8f38\u5165\u6846\u7684\u72c0\u614b\u662f\u7531 React \u7684 state \u4f86\u63a7\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5c07\u6587\u672c\u8f38\u5165\u8a2d\u7f6e\u70ba\u53d7\u63a7\u5236\u7684\u8f38\u5165\uff1a<\/p>\n<pre class=\"brush: javascript\">\nclass MyComponent extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = {\n      value: ''\n    };\n  }\n\n  handleChange = (event) => {\n    this.setState({\n      value: event.target.value\n    });\n  }\n\n  render() {\n    return (\n      <input\n        type=\"text\"\n        value={this.state.value}\n        onChange={this.handleChange}\n      \/>\n    );\n  }\n}\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u8f38\u5165\u6846\u7684\u503c\u7531 `this.state.value` \u63a7\u5236\uff0c\u4e26\u4e14\u901a\u904e `onChange` \u4e8b\u4ef6\u8655\u7406\u5668\u4f86\u66f4\u65b0 state\u3002\u9019\u6a23\u5c31\u80fd\u5920\u9632\u6b62\u5f15\u767c\u300c\u672a\u63a7\u5236\u8f38\u5165\u8f49\u70ba\u53d7\u63a7\u5236\u8f38\u5165\u300d\u7684\u932f\u8aa4\u3002<\/p>\n<h2>\u6700\u4f73\u5be6\u8e10\uff1a\u4fdd\u6301\u4e00\u81f4\u6027<\/h2>\n<p>\u5728\u4f7f\u7528 React.js \u6642\uff0c\u4fdd\u6301\u8f38\u5165\u6846\u7684\u63a7\u5236\u65b9\u5f0f\u4e00\u81f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u78ba\u4fdd\u6240\u6709\u7684\u8868\u55ae\u5143\u7d20\u90fd\u4f7f\u7528\u53d7\u63a7\u5236\u7d44\u4ef6\uff0c\u9019\u6a23\u80fd\u5920\u6e1b\u5c11\u932f\u8aa4\u767c\u751f\u7684\u6a5f\u6703\uff0c\u4e26\u63d0\u9ad8\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u63a2\u8a0e\u4e86 React.js \u4e2d\u300c\u672a\u63a7\u5236\u8f38\u5165\u8f49\u70ba\u53d7\u63a7\u5236\u8f38\u5165\u300d\u7684\u932f\u8aa4\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904e\u5c07\u8f38\u5165\u8a2d\u7f6e\u70ba\u53d7\u63a7\u5236\u7d44\u4ef6\uff0c\u4e26\u4fdd\u6301\u4e00\u81f4\u7684\u63a7\u5236\u65b9\u5f0f\uff0c\u4f60\u5c07\u80fd\u5920\u907f\u514d\u9019\u985e\u932f\u8aa4\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5920\u5e6b\u52a9\u4f60\u5728 React.js \u7684\u958b\u767c\u904e\u7a0b\u4e2d\u66f4\u52a0\u5f97\u5fc3\u61c9\u624b\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u89e3\u6c7aReact.js\u4e2d\u300cError:Acomponentischanginganuncontrolledinputoftypetexttobecontrolled\u300d\u7684\u932f\u8aa4\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u6709\u7528\u7684\u6280\u5de7\uff0c\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u89e3\u6c7a\u6b64\u985e\u554f\u984c\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-4727","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-1ef","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4727","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=4727"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4727\/revisions"}],"predecessor-version":[{"id":4728,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4727\/revisions\/4728"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}