{"id":4766,"date":"2023-01-04T18:15:03","date_gmt":"2023-01-04T10:15:03","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4766"},"modified":"2023-01-04T18:15:03","modified_gmt":"2023-01-04T10:15:03","slug":"%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e3%80%8ccanonlyupdateamountedormountingcomponent%e3%80%8d%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%8ccanonlyupdateamountedormountingcomponent%e3%80%8d%e9%8c%af%e8%aa%a4\/","title":{"rendered":"\u89e3\u6c7aReact.js\u4e2d\u300cCanonlyupdateamountedormountingcomponent\u300d\u932f\u8aa4"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.js, Error, setState, Can only update a mounted or mounting component, unmounted component, no-op, undefined component\"><\/p>\n<h1>React.js &#8220;Error: setState(&#8230;): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.&#8221; \u932f\u8aa4\u89e3\u6c7a\u65b9\u6cd5<\/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\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff1a\u201cError: setState(&#8230;): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.\u201d \u9019\u500b\u932f\u8aa4\u7684\u539f\u56e0\u662f\u4ec0\u9ebc\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u6c7a\u5b83\uff1f<\/p>\n<h2>\u4ec0\u9ebc\u662f setState()\uff1f<\/h2>\n<p>setState() \u662f React.js \u4e2d\u7684\u4e00\u500b\u91cd\u8981\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u65b0\u7d44\u4ef6\u7684\u72c0\u614b\uff0c\u4ee5\u53ca\u66f4\u65b0 UI\u3002\u5b83\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: javascript\">\nthis.setState({\n  [state]: value\n});\n<\/pre>\n<p>setState() \u65b9\u6cd5\u6703\u89f8\u767c\u4e00\u500b\u91cd\u65b0\u6e32\u67d3\u7684\u904e\u7a0b\uff0c\u4ee5\u4fbf\u66f4\u65b0 UI\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f &#8220;Error: setState(&#8230;): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.&#8221; \u932f\u8aa4\uff1f<\/h2>\n<p>\u9019\u500b\u932f\u8aa4\u8868\u660e\uff0c\u4f60\u6b63\u5728\u5617\u8a66\u5728\u4e00\u500b\u672a\u5b89\u88dd\u7684\u7d44\u4ef6\u4e0a\u8abf\u7528 setState() \u65b9\u6cd5\uff0c\u800c\u9019\u662f\u4e0d\u5141\u8a31\u7684\u3002\u9019\u500b\u932f\u8aa4\u7684\u539f\u56e0\u662f\uff0c\u7576\u4e00\u500b\u7d44\u4ef6\u88ab\u5378\u8f09\u6642\uff0c\u5b83\u7684\u72c0\u614b\u5c31\u4e0d\u80fd\u88ab\u66f4\u65b0\u4e86\uff0c\u56e0\u6b64\u8abf\u7528 setState() \u65b9\u6cd5\u5c31\u6703\u51fa\u932f\u3002<\/p>\n<h2>\u5982\u4f55\u89e3\u6c7a &#8220;Error: setState(&#8230;): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.&#8221; \u932f\u8aa4\uff1f<\/h2>\n<p>\u8981\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff0c\u4f60\u9700\u8981\u78ba\u4fdd\u4f60\u53ea\u5728\u5b89\u88dd\u7684\u7d44\u4ef6\u4e0a\u8abf\u7528 setState() \u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 React.js \u7684\u751f\u547d\u9031\u671f\u65b9\u6cd5\u4f86\u78ba\u4fdd\u7d44\u4ef6\u5df2\u7d93\u5b89\u88dd\uff0c\u7136\u5f8c\u518d\u8abf\u7528 setState() \u65b9\u6cd5\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5728 componentDidMount() \u751f\u547d\u9031\u671f\u65b9\u6cd5\u4e2d\u8abf\u7528 setState() \u65b9\u6cd5\uff0c\u4ee5\u78ba\u4fdd\u7d44\u4ef6\u5df2\u7d93\u5b89\u88dd\uff1a<\/p>\n<pre class=\"brush: javascript\">\ncomponentDidMount() {\n  this.setState({\n    [state]: value\n  });\n}\n<\/pre>\n<p>\u53e6\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 componentWillUnmount() \u751f\u547d\u9031\u671f\u65b9\u6cd5\u4f86\u78ba\u4fdd\u7d44\u4ef6\u5728\u5378\u8f09\u4e4b\u524d\u4e0d\u6703\u8abf\u7528 setState() \u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: javascript\">\ncomponentWillUnmount() {\n  this.setState({\n    [state]: value\n  });\n}\n<\/pre>\n<p>\u901a\u904e\u4f7f\u7528 React.js \u7684\u751f\u547d\u9031\u671f\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u78ba\u4fdd\u53ea\u5728\u5b89\u88dd\u7684\u7d44\u4ef6\u4e0a\u8abf\u7528 setState() \u65b9\u6cd5\uff0c\u4ee5\u907f\u514d\u51fa\u73fe\u201cError: setState(&#8230;): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.\u201d \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 React.js \u4e2d\u7684\u4e00\u500b\u5e38\u898b\u932f\u8aa4\uff1a\u201cError: setState(&#8230;): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.\u201d \u9019\u500b\u932f\u8aa4\u7684\u539f\u56e0\u662f\u4ec0\u9ebc\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u6c7a\u5b83\u3002\u6211\u5011\u767c\u73fe\uff0c\u9019\u500b\u932f\u8aa4\u662f\u7531\u65bc\u4f60\u6b63\u5728\u5617\u8a66\u5728\u4e00\u500b\u672a\u5b89\u88dd\u7684\u7d44\u4ef6\u4e0a\u8abf\u7528 setState() \u65b9\u6cd5\uff0c\u800c\u9019\u662f\u4e0d\u5141\u8a31\u7684\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u500b\u932f\u8aa4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 React.js \u7684\u751f\u547d\u9031\u671f\u65b9\u6cd5\u4f86\u78ba\u4fdd\u7d44\u4ef6\u5df2\u7d93\u5b89\u88dd\uff0c\u7136\u5f8c\u518d\u8abf\u7528 setState() \u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u89e3\u6c7aReact.js\u4e2d\u300cCanonlyupdateamountedormountingcomponent\u300d\u932f\u8aa4\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u6709\u7528\u7684\u6280\u5de7\uff0c\u8b93\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-4766","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-1eS","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4766","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=4766"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4766\/revisions"}],"predecessor-version":[{"id":4767,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4766\/revisions\/4767"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}