{"id":4692,"date":"2023-01-04T17:38:30","date_gmt":"2023-01-04T09:38:30","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4692"},"modified":"2023-01-04T17:38:30","modified_gmt":"2023-01-04T09:38:30","slug":"%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e7%9a%84erroracomponentischanginganuncontrolledinputoftyperadiotobecontrolled%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%e7%9a%84erroracomponentischanginganuncontrolledinputoftyperadiotobecontrolled%e9%8c%af%e8%aa%a4\/","title":{"rendered":"\u89e3\u6c7aReact.js\u4e2d\u7684&#8221;Error:Acomponentischanginganuncontrolledinputoftyperadiotobecontrolled&#8221;\u932f\u8aa4"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.js, Error, uncontrolled input, controlled, radio\"><\/p>\n<h1>React.js \u4e2d\u7684 &#8220;Error: A component is changing an uncontrolled input of type radio to be controlled.&#8221; \u932f\u8aa4<\/h1>\n<p>React.js \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u7aef\u61c9\u7528\u7a0b\u5e8f\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002\u7136\u800c\uff0c\u5728\u4f7f\u7528 React.js \u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u932f\u8aa4\uff0c\u5176\u4e2d\u4e00\u500b\u662f &#8220;Error: A component is changing an uncontrolled input of type radio to be controlled.&#8221; \u932f\u8aa4\u3002<\/p>\n<p>\u9019\u500b\u932f\u8aa4\u8868\u660e\uff0c\u60a8\u6b63\u5728\u5617\u8a66\u5c07\u4e00\u500b\u672a\u53d7\u63a7\u5236\u7684\u8f38\u5165\uff08\u4f8b\u5982 radio \u6309\u9215\uff09\u66f4\u6539\u70ba\u53d7\u63a7\u5236\u7684\u8f38\u5165\u3002\u9019\u662f\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4e3b\u8981\u662f\u56e0\u70ba React.js \u5c0d\u65bc\u53d7\u63a7\u5236\u7684\u8f38\u5165\u6709\u4e00\u4e9b\u7279\u5b9a\u7684\u8981\u6c42\uff0c\u800c\u958b\u767c\u8005\u53ef\u80fd\u6c92\u6709\u9075\u5faa\u9019\u4e9b\u8981\u6c42\u3002<\/p>\n<h2>\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u8981\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684\u4ee3\u78bc\u7b26\u5408 React.js \u7684\u8981\u6c42\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684\u4ee3\u78bc\u4e2d\u6709\u4e00\u500b <code>value<\/code> \u5c6c\u6027\uff0c\u5b83\u6307\u5b9a\u4e86\u8f38\u5165\u7684\u503c\u3002\u5176\u6b21\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684\u4ee3\u78bc\u4e2d\u6709\u4e00\u500b <code>onChange<\/code> \u4e8b\u4ef6\uff0c\u5b83\u6703\u5728\u7528\u6236\u5c0d\u8f38\u5165\u9032\u884c\u66f4\u6539\u6642\u89f8\u767c\u3002\u6700\u5f8c\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684\u4ee3\u78bc\u4e2d\u6709\u4e00\u500b <code>checked<\/code> \u5c6c\u6027\uff0c\u5b83\u6307\u5b9a\u4e86\u8f38\u5165\u662f\u5426\u88ab\u9078\u4e2d\u3002<\/p>\n<p>\u70ba\u4e86\u78ba\u4fdd\u60a8\u7684\u4ee3\u78bc\u7b26\u5408 React.js \u7684\u8981\u6c42\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre class=\"brush: javascript\">\n<input \n  type=\"radio\" \n  value=\"option1\" \n  checked={this.state.value === 'option1'} \n  onChange={this.handleChange} \n\/>\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>value<\/code> \u5c6c\u6027\u6307\u5b9a\u4e86\u8f38\u5165\u7684\u503c\uff0c\u4f7f\u7528 <code>onChange<\/code> \u4e8b\u4ef6\u4f86\u8655\u7406\u7528\u6236\u5c0d\u8f38\u5165\u7684\u66f4\u6539\uff0c\u4e26\u4f7f\u7528 <code>checked<\/code> \u5c6c\u6027\u4f86\u6307\u5b9a\u8f38\u5165\u662f\u5426\u88ab\u9078\u4e2d\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9075\u5faa\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u5c31\u53ef\u4ee5\u89e3\u6c7a &#8220;Error: A component is changing an uncontrolled input of type radio to be controlled.&#8221; \u932f\u8aa4\uff0c\u4e26\u78ba\u4fdd\u60a8\u7684\u4ee3\u78bc\u7b26\u5408 React.js \u7684\u8981\u6c42\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 &#8220;Error: A component is changing an uncontrolled input of type radio to be controlled.&#8221; \u932f\u8aa4\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u6c7a\u8a72\u932f\u8aa4\u7684\u65b9\u6cd5\u3002\u6211\u5011\u5efa\u8b70\u60a8\u78ba\u4fdd\u60a8\u7684\u4ee3\u78bc\u4e2d\u6709\u4e00\u500b <code>value<\/code> \u5c6c\u6027\uff0c\u4e00\u500b <code>onChange<\/code> \u4e8b\u4ef6\uff0c\u4ee5\u53ca\u4e00\u500b <code>checked<\/code> \u5c6c\u6027\uff0c\u4ee5\u78ba\u4fdd\u60a8\u7684\u4ee3\u78bc\u7b26\u5408 React.js \u7684\u8981\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u89e3\u6c7aReact.js\u4e2d\u7684&#8221;Error:Acomponentischanginganuncontrolledinputoftyperadiotobecontrolled&#8221;\u932f\u8aa4\uff0c\u4e26\u77ad\u89e3\u5982\u4f55\u4f7f\u7528React.js\u4e2d\u7684controlledinputs\u4f86\u907f\u514d\u6b64\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-4692","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-1dG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4692","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=4692"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4692\/revisions"}],"predecessor-version":[{"id":4693,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4692\/revisions\/4693"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}