{"id":4672,"date":"2023-01-04T17:27:24","date_gmt":"2023-01-04T09:27:24","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4672"},"modified":"2023-01-04T17:27:24","modified_gmt":"2023-01-04T09:27:24","slug":"%e4%ba%86%e8%a7%a3react-js%e4%b8%ad%e7%9a%84hooks%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e4%ba%86%e8%a7%a3react-js%e4%b8%ad%e7%9a%84hooks%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3React.js\u4e2d\u7684Hooks\u51fd\u6578\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.js, Error, Invalid hook call, Hooks, Function Component\"><\/p>\n<h1>\u89e3\u6c7a React.js &#8220;Error: Invalid hook call. Hooks can only be called inside the body of a function component.&#8221; \u932f\u8aa4<\/h1>\n<p>React.js \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\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\u554f\u984c\uff0c\u5176\u4e2d\u4e00\u500b\u662f &#8220;Error: Invalid hook call. Hooks can only be called inside the body of a function component.&#8221; \u932f\u8aa4\u3002<\/p>\n<p>\u9019\u500b\u932f\u8aa4\u8868\u660e\uff0c\u958b\u767c\u8005\u6b63\u5728\u5617\u8a66\u5728\u975e\u51fd\u6578\u7d44\u4ef6\u4e2d\u8abf\u7528 Hooks\uff0c\u800c Hooks \u53ea\u80fd\u5728\u51fd\u6578\u7d44\u4ef6\u4e2d\u8abf\u7528\u3002\u56e0\u6b64\uff0c\u8981\u89e3\u6c7a\u6b64\u932f\u8aa4\uff0c\u958b\u767c\u8005\u9700\u8981\u5c07 Hooks \u79fb\u52d5\u5230\u51fd\u6578\u7d44\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u6b63\u78ba\u8abf\u7528\u5b83\u5011\u3002<\/p>\n<h2>\u70ba\u4ec0\u9ebc Hooks \u53ea\u80fd\u5728\u51fd\u6578\u7d44\u4ef6\u4e2d\u8abf\u7528\uff1f<\/h2>\n<p>Hooks \u662f React 16.8 \u7248\u672c\u4e2d\u63a8\u51fa\u7684\u4e00\u7a2e\u65b0\u529f\u80fd\uff0c\u5b83\u5141\u8a31\u958b\u767c\u8005\u5728\u51fd\u6578\u7d44\u4ef6\u4e2d\u4f7f\u7528\u72c0\u614b\u548c\u5176\u4ed6 React \u7279\u6027\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528 class \u7d44\u4ef6\u3002Hooks \u53ea\u80fd\u5728\u51fd\u6578\u7d44\u4ef6\u4e2d\u8abf\u7528\uff0c\u56e0\u70ba\u5b83\u5011\u662f\u57fa\u65bc\u51fd\u6578\u7684\uff0c\u800c\u4e0d\u662f\u57fa\u65bc\u5c0d\u8c61\u7684\u3002\u56e0\u6b64\uff0c\u5982\u679c\u958b\u767c\u8005\u5617\u8a66\u5728\u975e\u51fd\u6578\u7d44\u4ef6\u4e2d\u8abf\u7528 Hooks\uff0c\u5c31\u6703\u51fa\u73fe &#8220;Error: Invalid hook call. Hooks can only be called inside the body of a function component.&#8221; \u932f\u8aa4\u3002<\/p>\n<h2>\u5982\u4f55\u89e3\u6c7a &#8220;Error: Invalid hook call. Hooks can only be called inside the body of a function component.&#8221; \u932f\u8aa4\uff1f<\/h2>\n<p>\u8981\u89e3\u6c7a &#8220;Error: Invalid hook call. Hooks can only be called inside the body of a function component.&#8221; \u932f\u8aa4\uff0c\u958b\u767c\u8005\u9700\u8981\u5c07 Hooks \u79fb\u52d5\u5230\u51fd\u6578\u7d44\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u6b63\u78ba\u8abf\u7528\u5b83\u5011\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u958b\u767c\u8005\u6b63\u5728\u5617\u8a66\u5728\u975e\u51fd\u6578\u7d44\u4ef6\u4e2d\u8abf\u7528 <span style=\"color: #009900; font-weight: bold;\">useState<\/span> Hook\uff0c\u4ed6\u5011\u53ef\u4ee5\u5c07\u5b83\u79fb\u52d5\u5230\u51fd\u6578\u7d44\u4ef6\u4e2d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"brush: javascript\">\nfunction MyComponent() {\n  const [count, setCount] = useState(0);\n\n  return (\n    <div>\n      You clicked {count} times\n      <button onClick={() => setCount(count + 1)}>\n        Click me\n      <\/button>\n    <\/div>\n  );\n}\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<span style=\"color: #009900; font-weight: bold;\">useState<\/span> Hook \u5df2\u7d93\u88ab\u79fb\u52d5\u5230\u51fd\u6578\u7d44\u4ef6\u4e2d\uff0c\u56e0\u6b64\u5c07\u4e0d\u6703\u51fa\u73fe &#8220;Error: Invalid hook call. Hooks can only be called inside the body of a function component.&#8221; \u932f\u8aa4\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u8a0e\u8ad6\u4e86 React.js \u4e2d\u7684 &#8220;Error: Invalid hook call. Hooks can only be called inside the body of a function component.&#8221; \u932f\u8aa4\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u6c7a\u5b83\u3002\u6211\u5011\u767c\u73fe\uff0c\u6b64\u932f\u8aa4\u8868\u660e\u958b\u767c\u8005\u6b63\u5728\u5617\u8a66\u5728\u975e\u51fd\u6578\u7d44\u4ef6\u4e2d\u8abf\u7528 Hooks\uff0c\u800c Hooks \u53ea\u80fd\u5728\u51fd\u6578\u7d44\u4ef6\u4e2d\u8abf\u7528\u3002\u56e0\u6b64\uff0c\u8981\u89e3\u6c7a\u6b64\u932f\u8aa4\uff0c\u958b\u767c\u8005\u9700\u8981\u5c07 Hooks \u79fb\u52d5\u5230\u51fd\u6578\u7d44\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u6b63\u78ba\u8abf\u7528\u5b83\u5011\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2React.js\u4e2d\u7684Hooks\u51fd\u6578\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u6709\u6548\u7387\u5730\u958b\u767c\u51fa\u66f4\u597d\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Hooks\u51fd\u6578\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u907f\u514d&#8221;Invalidhookcall&#8221;\u7684\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-4672","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-1dm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4672","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=4672"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4672\/revisions"}],"predecessor-version":[{"id":4673,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4672\/revisions\/4673"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}