{"id":4670,"date":"2023-01-04T17:26:18","date_gmt":"2023-01-04T09:26:18","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4670"},"modified":"2023-01-04T17:26:18","modified_gmt":"2023-01-04T09:26:18","slug":"%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e3%80%8cstatemutation%e3%80%8d%e9%8c%af%e8%aa%a4%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e8%a7%a3%e6%b1%bareact-js%e4%b8%ad%e3%80%8cstatemutation%e3%80%8d%e9%8c%af%e8%aa%a4%e7%9a%84%e6%96%b9%e6%b3%95\/","title":{"rendered":"\u89e3\u6c7aReact.js\u4e2d\u300cStateMutation\u300d\u932f\u8aa4\u7684\u65b9\u6cd5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.js, Error, State Mutation, Dispatch\"><\/p>\n<h1>\u89e3\u6c7a React.js &#8220;Error: A state mutation was detected between dispatches&#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\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff1a\u201cError\uff1aA state mutation was detected between dispatches\u201d\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u8a73\u7d30\u89e3\u91cb\u9019\u500b\u932f\u8aa4\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u89e3\u6c7a\u65b9\u6848\uff0c\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u89e3\u6c7a\u6b64\u554f\u984c\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u201cError\uff1aA state mutation was detected between dispatches\u201d\u932f\u8aa4\uff1f<\/h2>\n<p>\u201cError\uff1aA state mutation was detected between dispatches\u201d\u932f\u8aa4\u662f React.js \u4e2d\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u5b83\u8868\u660e\u5728\u6d3e\u9063\u5169\u500b\u6d3e\u9063\u4e4b\u9593\u6aa2\u6e2c\u5230\u72c0\u614b\u8b8a\u5316\u3002\u9019\u610f\u5473\u8457\u5728\u6d3e\u9063\u4e4b\u9593\uff0c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u88ab\u66f4\u6539\u4e86\uff0c\u800c\u4e0d\u662f\u4f7f\u7528 React.js \u7684 setState() \u65b9\u6cd5\u3002<\/p>\n<p>\u9019\u500b\u932f\u8aa4\u53ef\u80fd\u6703\u51fa\u73fe\u5728\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u4e2d\uff0c\u56e0\u70ba\u60a8\u53ef\u80fd\u6703\u5728\u6d3e\u9063\u4e4b\u9593\u66f4\u6539\u72c0\u614b\uff0c\u800c\u4e0d\u662f\u4f7f\u7528 React.js \u7684 setState() \u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5728\u6d3e\u9063\u4e4b\u9593\u66f4\u6539\u4e86\u61c9\u7528\u7a0b\u5e8f\u7684\u5168\u5c40\u8b8a\u91cf\uff0c\u5247\u53ef\u80fd\u6703\u51fa\u73fe\u6b64\u932f\u8aa4\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u5982\u4f55\u89e3\u6c7a\u201cError\uff1aA state mutation was detected between dispatches\u201d\u932f\u8aa4\uff1f<\/h2>\n<p>\u89e3\u6c7a\u201cError\uff1aA state mutation was detected between dispatches\u201d\u932f\u8aa4\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u78ba\u4fdd\u60a8\u5728\u6d3e\u9063\u4e4b\u9593\u53ea\u4f7f\u7528 React.js \u7684 setState() \u65b9\u6cd5\u4f86\u66f4\u6539\u72c0\u614b\u3002\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u662f\u53ef\u9810\u6e2c\u7684\uff0c\u4e26\u4e14\u53ef\u4ee5\u6b63\u78ba\u5730\u66f4\u65b0 UI\u3002<\/p>\n<p>\u53e6\u4e00\u500b\u89e3\u6c7a\u65b9\u6848\u662f\u4f7f\u7528 React.js \u7684 useReducer() \u65b9\u6cd5\u4f86\u66f4\u65b0\u72c0\u614b\u3002 useReducer() \u65b9\u6cd5\u53ef\u4ee5\u8b93\u60a8\u5728\u6d3e\u9063\u4e4b\u9593\u66f4\u65b0\u72c0\u614b\uff0c\u800c\u4e0d\u6703\u51fa\u73fe\u201cError\uff1aA state mutation was detected between dispatches\u201d\u932f\u8aa4\u3002<\/p>\n<p>\u70ba\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528 useReducer() \u65b9\u6cd5\uff0c\u8acb\u53c3\u8003\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst [state, dispatch] = useReducer(reducer, initialState);\n\n\/\/ Dispatch an action\ndispatch({ type: 'INCREMENT' });\n\n\/\/ Update the state\nfunction reducer(state, action) {\n  switch (action.type) {\n    case 'INCREMENT':\n      return { count: state.count + 1 };\n    default:\n      return state;\n  }\n}\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 useReducer() \u65b9\u6cd5\u4f86\u66f4\u65b0\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u3002\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b reducer \u51fd\u6578\uff0c\u8a72\u51fd\u6578\u63a5\u6536\u4e00\u500b action \u5c0d\u8c61\uff0c\u4e26\u6839\u64da action \u5c0d\u8c61\u7684\u985e\u578b\u4f86\u66f4\u65b0\u72c0\u614b\u3002\u7136\u5f8c\uff0c\u6211\u5011\u4f7f\u7528 dispatch() \u65b9\u6cd5\u4f86\u6d3e\u9063\u4e00\u500b action \u5c0d\u8c61\uff0c\u4ee5\u66f4\u65b0\u72c0\u614b\u3002<\/p>\n<p>\u4f7f\u7528 useReducer() \u65b9\u6cd5\u53ef\u4ee5\u8b93\u60a8\u5728\u6d3e\u9063\u4e4b\u9593\u66f4\u65b0\u72c0\u614b\uff0c\u800c\u4e0d\u6703\u51fa\u73fe\u201cError\uff1aA state mutation was detected between dispatches\u201d\u932f\u8aa4\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u8a73\u7d30\u89e3\u91cb\u4e86 React.js \u4e2d\u7684\u201cError\uff1aA state mutation was detected between dispatches\u201d\u932f\u8aa4\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u4e9b\u89e3\u6c7a\u65b9\u6848\uff0c\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u89e3\u6c7a\u6b64\u554f\u984c\u3002\u6211\u5011\u5efa\u8b70\u60a8\u5728\u6d3e\u9063\u4e4b\u9593\u53ea\u4f7f\u7528 React.js \u7684 setState() \u65b9\u6cd5\u4f86\u66f4\u6539\u72c0\u614b\uff0c\u6216\u8005\u4f7f\u7528 React.js \u7684 useReducer() \u65b9\u6cd5\u4f86\u66f4\u65b0\u72c0\u614b\u3002<\/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\u300cStateMutation\u300d\u932f\u8aa4\u7684\u554f\u984c\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u4f7f\u7528React.js\u4f86\u958b\u767c\u61c9\u7528\u7a0b\u5f0f\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-4670","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-1dk","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4670","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=4670"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4670\/revisions"}],"predecessor-version":[{"id":4671,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4670\/revisions\/4671"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}