{"id":4680,"date":"2023-01-04T17:32:07","date_gmt":"2023-01-04T09:32:07","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4680"},"modified":"2023-01-04T17:32:07","modified_gmt":"2023-01-04T09:32:07","slug":"%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e3%80%8c%e5%a4%aa%e5%a4%9a%e9%87%8d%e6%96%b0%e6%b8%b2%e6%9f%93%e3%80%8d%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%ad%e3%80%8c%e5%a4%aa%e5%a4%9a%e9%87%8d%e6%96%b0%e6%b8%b2%e6%9f%93%e3%80%8d%e7%9a%84%e9%8c%af%e8%aa%a4\/","title":{"rendered":"\u89e3\u6c7aReact.js\u4e2d\u300c\u592a\u591a\u91cd\u65b0\u6e32\u67d3\u300d\u7684\u932f\u8aa4"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.js, Error, Too many re-renders, React, Infinite loop\"><\/p>\n<h1>\u89e3\u6c7a React.js &#8220;Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.&#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\u6709\u6642\u5019\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff1a\u201cError\uff1aToo many re-renders\u3002React \u9650\u5236\u4e86\u6e32\u67d3\u6b21\u6578\u4ee5\u9632\u6b62\u7121\u9650\u5faa\u74b0\u201d\u3002<\/p>\n<p>\u9019\u500b\u932f\u8aa4\u901a\u5e38\u662f\u7531\u65bc React \u7684\u7d44\u4ef6\u5728\u6e32\u67d3\u6642\u51fa\u73fe\u4e86\u7121\u9650\u5faa\u74b0\uff0c\u5c0e\u81f4 React \u7121\u6cd5\u6b63\u78ba\u5730\u6e32\u67d3\u7d44\u4ef6\u3002\u9019\u7a2e\u60c5\u6cc1\u901a\u5e38\u662f\u7531\u65bc\u958b\u767c\u8005\u5728\u7d44\u4ef6\u4e2d\u4f7f\u7528\u4e86\u7121\u7aae\u905e\u8ff4\u51fd\u6578\uff0c\u5c0e\u81f4 React \u7121\u6cd5\u6b63\u78ba\u5730\u6e32\u67d3\u7d44\u4ef6\u3002<\/p>\n<h2>\u89e3\u6c7a\u8a72\u554f\u984c\u7684\u65b9\u6cd5<\/h2>\n<p>\u8981\u89e3\u6c7a\u8a72\u554f\u984c\uff0c\u958b\u767c\u8005\u9700\u8981\u6aa2\u67e5\u7d44\u4ef6\u4e2d\u7684\u4ee3\u78bc\uff0c\u78ba\u4fdd\u6c92\u6709\u7121\u7aae\u905e\u8ff4\u51fd\u6578\u3002\u5982\u679c\u767c\u73fe\u6709\u7121\u7aae\u905e\u8ff4\u51fd\u6578\uff0c\u5247\u9700\u8981\u5c07\u5176\u4fee\u6539\u70ba\u975e\u7121\u7aae\u905e\u8ff4\u51fd\u6578\uff0c\u4ee5\u4fbf React \u53ef\u4ee5\u6b63\u78ba\u5730\u6e32\u67d3\u7d44\u4ef6\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u4f7f\u7528 React \u7684 shouldComponentUpdate() \u65b9\u6cd5\u4f86\u63a7\u5236\u7d44\u4ef6\u7684\u6e32\u67d3\uff0c\u4ee5\u907f\u514d\u7121\u9650\u5faa\u74b0\u7684\u554f\u984c\u3002shouldComponentUpdate() \u65b9\u6cd5\u53ef\u4ee5\u63a5\u53d7\u5169\u500b\u53c3\u6578\uff0c\u4e00\u500b\u662f\u65b0\u7684 props\uff0c\u53e6\u4e00\u500b\u662f\u65b0\u7684 state\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u5e03\u723e\u503c\uff0c\u6307\u793a React \u662f\u5426\u61c9\u8a72\u66f4\u65b0\u7d44\u4ef6\u3002<\/p>\n<pre class=\"brush: javascript\">\nshouldComponentUpdate(nextProps, nextState) {\n    if (this.props.value !== nextProps.value) {\n        return true;\n    }\n    return false;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u78bc\u6aa2\u67e5\u65b0\u7684 props \u662f\u5426\u8207\u7576\u524d\u7684 props \u4e0d\u540c\uff0c\u5982\u679c\u4e0d\u540c\uff0c\u5247\u8fd4\u56de true\uff0c\u8868\u793a React \u61c9\u8a72\u66f4\u65b0\u7d44\u4ef6\uff0c\u5426\u5247\u8fd4\u56de false\uff0c\u8868\u793a React \u4e0d\u61c9\u8a72\u66f4\u65b0\u7d44\u4ef6\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u4f7f\u7528 React \u7684 PureComponent \u985e\uff0c\u5b83\u6703\u81ea\u52d5\u6aa2\u67e5 props \u548c state \u662f\u5426\u6709\u8b8a\u5316\uff0c\u5982\u679c\u6c92\u6709\u8b8a\u5316\uff0c\u5247\u4e0d\u6703\u66f4\u65b0\u7d44\u4ef6\u3002<\/p>\n<pre class=\"brush: javascript\">\nclass MyComponent extends React.PureComponent {\n    render() {\n        \/\/ ...\n    }\n}\n<\/pre>\n<p>\u901a\u904e\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236 React \u7d44\u4ef6\u7684\u6e32\u67d3\uff0c\u4ee5\u907f\u514d\u51fa\u73fe\u201cError\uff1aToo many re-renders\u3002React \u9650\u5236\u4e86\u6e32\u67d3\u6b21\u6578\u4ee5\u9632\u6b62\u7121\u9650\u5faa\u74b0\u201d\u7684\u932f\u8aa4\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\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\u6709\u6642\u5019\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff1a\u201cError\uff1aToo many re-renders\u3002React \u9650\u5236\u4e86\u6e32\u67d3\u6b21\u6578\u4ee5\u9632\u6b62\u7121\u9650\u5faa\u74b0\u201d\u3002\u8981\u89e3\u6c7a\u8a72\u554f\u984c\uff0c\u958b\u767c\u8005\u9700\u8981\u6aa2\u67e5\u7d44\u4ef6\u4e2d\u7684\u4ee3\u78bc\uff0c\u78ba\u4fdd\u6c92\u6709\u7121\u7aae\u905e\u8ff4\u51fd\u6578\uff0c\u4e26\u4f7f\u7528 React \u7684 shouldComponentUpdate() \u65b9\u6cd5\u548c PureComponent \u985e\u4f86\u63a7\u5236\u7d44\u4ef6\u7684\u6e32\u67d3\uff0c\u4ee5\u907f\u514d\u7121\u9650\u5faa\u74b0\u7684\u554f\u984c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3React.js\u4e2d\u300c\u592a\u591a\u91cd\u65b0\u6e32\u67d3\u300d\u7684\u932f\u8aa4\uff0c\u4ee5\u53ca\u5982\u4f55\u907f\u514d\u7121\u9650\u5faa\u74b0\u7684\u932f\u8aa4\uff0c\u4ee5\u78ba\u4fdd\u61c9\u7528\u7a0b\u5e8f\u7684\u6b63\u5e38\u904b\u884c\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-4680","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-1du","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4680","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=4680"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4680\/revisions"}],"predecessor-version":[{"id":4681,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4680\/revisions\/4681"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}