{"id":4778,"date":"2023-01-04T18:21:16","date_gmt":"2023-01-04T10:21:16","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4778"},"modified":"2023-01-05T09:55:02","modified_gmt":"2023-01-05T01:55:02","slug":"%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e7%9a%84elementrefwasspecifiedasastring%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%84elementrefwasspecifiedasastring%e9%8c%af%e8%aa%a4\/","title":{"rendered":"\u89e3\u6c7aReact.js\u4e2d\u7684&#8221;Elementrefwasspecifiedasastring&#8221;\u932f\u8aa4"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.js, Error, Element ref, React, Loaded, Details, React-refs-must-have-owner\"><\/p>\n<h1>\u89e3\u6c7a React.js &#8220;Error: Element ref was specified as a string (ref=&#8217;myInput&#8217;) but no owner was set. You may have multiple copies of React loaded. (details: react-refs-must-have-owner).&#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\u6709\u6642\u5019\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u554f\u984c\uff0c\u5176\u4e2d\u4e00\u500b\u662f &#8220;Error: Element ref was specified as a string (ref=&#8217;myInput&#8217;) but no owner was set. You may have multiple copies of React loaded. (details: react-refs-must-have-owner).&#8221; \u932f\u8aa4\u3002<\/p>\n<p>\u9019\u500b\u932f\u8aa4\u7684\u539f\u56e0\u662f\uff0c\u7576\u4f60\u5728 React \u7684\u7d44\u4ef6\u4e2d\u4f7f\u7528 ref \u5c6c\u6027\u6642\uff0c\u4f60\u5fc5\u9808\u78ba\u4fdd\u5b83\u6709\u4e00\u500b owner\uff0c\u5426\u5247\u5c31\u6703\u51fa\u73fe\u9019\u500b\u932f\u8aa4\u3002<\/p>\n<h2>\u89e3\u6c7a\u65b9\u6cd5<\/h2>\n<p>\u8981\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u4f60\u9700\u8981\u78ba\u4fdd\u4f60\u7684 React \u7d44\u4ef6\u6709\u4e00\u500b owner\uff0c\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u5169\u7a2e\u65b9\u5f0f\u4f86\u5be6\u73fe\uff1a<\/p>\n<ul>\n<li>\n        <strong>\u4f7f\u7528 React.createRef() \u65b9\u6cd5\uff1a<\/strong> \u4f60\u53ef\u4ee5\u4f7f\u7528 React.createRef() \u65b9\u6cd5\u4f86\u5275\u5efa\u4e00\u500b ref\uff0c\u4e26\u5c07\u5176\u50b3\u905e\u7d66\u7d44\u4ef6\uff0c\u4ee5\u78ba\u4fdd\u5b83\u6709\u4e00\u500b owner\u3002<\/p>\n<pre class=\"brush: javascript\">\nclass MyComponent extends React.Component {\n  constructor(props) {\n    super(props);\n    this.myRef = React.createRef();\n  }\n  render() {\n    return <div ref={this.myRef} \/>;\n  }\n}\n<\/pre>\n<\/li>\n<li>\n        <strong>\u4f7f\u7528\u56de\u8abf\u51fd\u6578\uff1a<\/strong> \u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u56de\u8abf\u51fd\u6578\u4f86\u5275\u5efa ref\uff0c\u4e26\u5c07\u5176\u50b3\u905e\u7d66\u7d44\u4ef6\uff0c\u4ee5\u78ba\u4fdd\u5b83\u6709\u4e00\u500b owner\u3002<\/p>\n<pre class=\"brush: javascript\">\nclass MyComponent extends React.Component {\n  constructor(props) {\n    super(props);\n    this.myRef = null;\n  }\n  setRef = (ref) => {\n    this.myRef = ref;\n  }\n  render() {\n    return <div ref={this.setRef} \/>;\n  }\n}\n<\/pre>\n<\/li>\n<\/ul>\n<p>\u9019\u5169\u7a2e\u65b9\u6cd5\u90fd\u53ef\u4ee5\u78ba\u4fdd\u4f60\u7684 React \u7d44\u4ef6\u6709\u4e00\u500b owner\uff0c\u4ee5\u89e3\u6c7a &#8220;Error: Element ref was specified as a string (ref=&#8217;myInput&#8217;) but no owner was set. You may have multiple copies of React loaded. (details: react-refs-must-have-owner).&#8221; \u932f\u8aa4\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u8a0e\u8ad6\u4e86\u5982\u4f55\u89e3\u6c7a React.js &#8220;Error: Element ref was specified as a string (ref=&#8217;myInput&#8217;) but no owner was set. You may have multiple copies of React loaded. (details: react-refs-must-have-owner).&#8221; \u932f\u8aa4\u3002\u6211\u5011\u63d0\u5230\u4e86\u5169\u7a2e\u89e3\u6c7a\u65b9\u6cd5\uff1a\u4f7f\u7528 React.createRef() \u65b9\u6cd5\u548c\u4f7f\u7528\u56de\u8abf\u51fd\u6578\u3002\u901a\u904e\u4f7f\u7528\u9019\u5169\u7a2e\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u78ba\u4fdd\u4f60\u7684 React \u7d44\u4ef6\u6709\u4e00\u500b owner\uff0c\u4ee5\u89e3\u6c7a\u9019\u500b\u554f\u984c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u89e3\u6c7aReact.js\u4e2d\u7684&#8221;Elementrefwasspecifiedasastring&#8221;\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-4778","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-1f4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4778","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=4778"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4778\/revisions"}],"predecessor-version":[{"id":4780,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4778\/revisions\/4780"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}