{"id":4686,"date":"2025-06-03T14:05:53","date_gmt":"2025-06-04T06:11:52","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4686"},"modified":"2025-06-04T14:05:53","modified_gmt":"2025-06-04T06:11:52","slug":"%e8%a7%a3%e6%b1%bareact-js%e4%b8%aderrorinvalidargumentsuppliedtooneofexpectedaninstanceofarray-%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%aderrorinvalidargumentsuppliedtooneofexpectedaninstanceofarray-%e7%9a%84%e9%8c%af%e8%aa%a4\/","title":{"rendered":"\u89e3\u6c7a React.js \u4e2d\u7684 &#8220;Error: Invalid argument supplied to oneOf, expected an instance of array.&#8221; \u932f\u8aa4\u7684\u7d42\u6975\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.js, \u932f\u8aa4\u6392\u9664, Invalid argument, oneOf, \u9663\u5217, JavaScript \u6559\u5b78\"><\/p>\n<h1>\u89e3\u6c7a React.js \u4e2d\u7684 &#8220;Error: Invalid argument supplied to oneOf, expected an instance of array.&#8221; \u932f\u8aa4\u7684\u7d42\u6975\u6307\u5357<\/h1>\n<p>\u5728\u4f7f\u7528 React.js \u958b\u767c\u61c9\u7528\u7a0b\u5e8f\u6642\uff0c\u958b\u767c\u8005\u5e38\u5e38\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\u3002\u7279\u5225\u662f\u7576\u4f60\u4f7f\u7528 `oneOf` \u65b9\u6cd5\u9032\u884c\u985e\u578b\u9a57\u8b49\u6642\uff0c\u53ef\u80fd\u6703\u51fa\u73fe\u201cError: Invalid argument supplied to oneOf, expected an instance of array.\u201d \u9019\u500b\u932f\u8aa4\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u70ba\u4f60\u63d0\u4f9b\u8a73\u7d30\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u5e6b\u52a9\u4f60\u6709\u6548\u5730\u6392\u9664\u9019\u500b\u932f\u8aa4\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u6b64\u932f\u8aa4\uff1f<\/h2>\n<p>\u201cError: Invalid argument supplied to oneOf, expected an instance of array.\u201d \u932f\u8aa4\u901a\u5e38\u8868\u793a\u4f60\u6b63\u5728\u5411 `oneOf` \u65b9\u6cd5\u50b3\u905e\u4e00\u500b\u4e0d\u6b63\u78ba\u7684\u985e\u578b\u2014\u2014\u975e\u9663\u5217\u7684\u503c\u3002\u9019\u901a\u5e38\u767c\u751f\u5728\u4f60\u671f\u671b\u63d0\u4f9b\u4e00\u500b\u503c\u7684\u9663\u5217\uff0c\u4f46\u5be6\u969b\u4e0a\u63d0\u4f9b\u4e86\u5b57\u4e32\u3001\u6578\u5b57\u6216\u5176\u4ed6\u985e\u578b\u7684\u8cc7\u6599\u6642\u3002<\/p>\n<h2>\u5982\u4f55\u89e3\u6c7a\u6b64\u932f\u8aa4\uff1f<\/h2>\n<p>\u8981\u4fee\u5fa9\u9019\u500b\u932f\u8aa4\uff0c\u9996\u5148\u4f60\u9700\u8981\u6aa2\u67e5\u4f60\u50b3\u905e\u7d66 `oneOf` \u65b9\u6cd5\u7684\u53c3\u6578\u985e\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<p>1. **\u5c07\u5b57\u4e32\u8f49\u63db\u70ba\u9663\u5217**\uff1a<br \/>\n   \u5982\u679c\u4f60\u6700\u521d\u7684\u8cc7\u6599\u662f\u5b57\u4e32\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 `split()` \u65b9\u6cd5\u5c07\u5176\u8f49\u63db\u70ba\u9663\u5217\u3002<\/p>\n<pre class=\"brush: javascript\">\n\/\/ \u5c07\u5b57\u4e32\u8f49\u63db\u70ba\u9663\u5217\nlet str = 'foo,bar,baz';\nlet arr = str.split(',');\n\n\/\/ \u5c07\u9663\u5217\u50b3\u905e\u7d66 oneOf \u65b9\u6cd5\noneOf(arr);\n   <\/pre>\n<p>2. **\u5c07\u6578\u5b57\u8f49\u63db\u70ba\u5b57\u4e32\u4e26\u8f49\u63db\u70ba\u9663\u5217**\uff1a<br \/>\n   \u7576\u4f60\u9700\u8981\u50b3\u905e\u7684\u503c\u662f\u6578\u5b57\u6642\uff0c\u5148\u5c07\u5176\u8f49\u63db\u70ba\u5b57\u4e32\uff0c\u7136\u5f8c\u518d\u8f49\u63db\u70ba\u9663\u5217\u3002<\/p>\n<pre class=\"brush: javascript\">\n\/\/ \u5c07\u6578\u5b57\u8f49\u63db\u70ba\u5b57\u4e32\nlet num = 123;\nlet str = num.toString();\n\n\/\/ \u5c07\u5b57\u4e32\u8f49\u63db\u70ba\u9663\u5217\nlet arr = [str];\n\n\/\/ \u5c07\u9663\u5217\u50b3\u905e\u7d66 oneOf \u65b9\u6cd5\noneOf(arr);\n   <\/pre>\n<p>3. **\u6aa2\u67e5\u50b3\u905e\u7684\u8cc7\u6599\u985e\u578b**\uff1a<br \/>\n   \u5728\u50b3\u905e\u4efb\u4f55\u8cc7\u6599\u4e4b\u524d\uff0c\u7e3d\u662f\u61c9\u8a72\u6aa2\u67e5\u5176\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u5b83\u7b26\u5408 `oneOf` \u65b9\u6cd5\u7684\u8981\u6c42\u3002<\/p>\n<pre class=\"brush: javascript\">\n\/\/ \u6aa2\u67e5\u503c\u7684\u985e\u578b\nconst value = getValue(); \/\/ \u5047\u8a2d\u9019\u500b\u51fd\u6578\u8fd4\u56de\u4f60\u8981\u6aa2\u67e5\u7684\u503c\n\nif (Array.isArray(value)) {\n    oneOf(value);\n} else {\n    console.error('Invalid type: Expected an array.');\n}\n   <\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u6df1\u5165\u63a2\u8a0e\u4e86\u5982\u4f55\u61c9\u5c0d React.js \u4e2d\u7684\u201cError: Invalid argument supplied to oneOf, expected an instance of array.\u201d \u932f\u8aa4\u3002\u78ba\u4fdd\u4f60\u50b3\u905e\u7d66 `oneOf` \u65b9\u6cd5\u7684\u662f\u6b63\u78ba\u7684\u985e\u578b\uff0c\u5fc5\u8981\u6642\u9032\u884c\u8f49\u63db\uff0c\u9019\u6a23\u53ef\u4ee5\u5927\u5927\u6e1b\u5c11\u51fa\u73fe\u6b64\u932f\u8aa4\u7684\u6a5f\u6703\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u5728\u958b\u767c\u904e\u7a0b\u4e2d\u66f4\u52a0\u9806\u5229\uff0c\u5feb\u901f\u89e3\u6c7a\u554f\u984c\uff01<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u89e3\u6c7aReact.js\u4e2d\u51fa\u73fe\u7684&#8221;Error:InvalidargumentsuppliedtooneOf,expectedaninstanceofarray.&#8221;\u932f\u8aa4\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u6709\u7528\u7684\u6280\u5de7\uff0c\u4ee5\u4fbf\u958b\u767c\u8005\u80fd\u5920\u66f4\u6709\u6548\u5730\u89e3\u6c7a\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-4686","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-1dA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4686","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=4686"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4686\/revisions"}],"predecessor-version":[{"id":4687,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4686\/revisions\/4687"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}