{"id":5971,"date":"2023-01-06T15:49:55","date_gmt":"2023-01-06T07:49:55","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5971"},"modified":"2023-01-06T15:49:55","modified_gmt":"2023-01-06T07:49:55","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react%e4%b8%ad%e7%9a%84usemutationeffect%e9%80%b2%e8%a1%8c%e5%89%af%e4%bd%9c%e7%94%a8%e8%99%95%e7%90%86","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react%e4%b8%ad%e7%9a%84usemutationeffect%e9%80%b2%e8%a1%8c%e5%89%af%e4%bd%9c%e7%94%a8%e8%99%95%e7%90%86\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528React\u4e2d\u7684useMutationEffect\u9032\u884c\u526f\u4f5c\u7528\u8655\u7406"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, useMutationEffect, \u526f\u4f5c\u7528\u5904\u7406\"><\/p>\n<p>React \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\u5728 React \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 useMutationEffect \u4f86\u8655\u7406\u526f\u4f5c\u7528\uff0c\u9019\u662f\u4e00\u500b React Hook\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u6bcf\u6b21\u6e32\u67d3\u5f8c\u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u4ec0\u9ebc\u662f\u526f\u4f5c\u7528\uff1f<\/h2>\n<p>\u526f\u4f5c\u7528\u662f\u6307\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u767c\u751f\u7684\u4efb\u4f55\u6539\u8b8a\uff0c\u5b83\u53ef\u4ee5\u662f\u5c0d\u61c9\u7528\u7a0b\u5e8f\u72c0\u614b\u7684\u66f4\u6539\uff0c\u4e5f\u53ef\u4ee5\u662f\u5c0d\u5916\u90e8\u7cfb\u7d71\u7684\u8acb\u6c42\uff0c\u4f8b\u5982\u5c0d\u6578\u64da\u5eab\u7684\u8acb\u6c42\u3002\u526f\u4f5c\u7528\u53ef\u4ee5\u662f\u540c\u6b65\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u7570\u6b65\u7684\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f useMutationEffect\uff1f<\/h2>\n<p>useMutationEffect \u662f React \u4e2d\u7684\u4e00\u500b Hook\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u6bcf\u6b21\u6e32\u67d3\u5f8c\u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002\u5b83\u8207 useEffect \u975e\u5e38\u76f8\u4f3c\uff0c\u4f46\u5b83\u53ea\u6703\u5728\u7d44\u4ef6\u7684\u6e32\u67d3\u5f8c\u57f7\u884c\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u7d44\u4ef6\u7684\u66f4\u65b0\u5f8c\u57f7\u884c\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 useMutationEffect \u8655\u7406\u526f\u4f5c\u7528\uff1f<\/h2>\n<p>\u4f7f\u7528 useMutationEffect \u4f86\u8655\u7406\u526f\u4f5c\u7528\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u5728\u7d44\u4ef6\u4e2d\u5b9a\u7fa9\u4e00\u500b\u51fd\u6578\uff0c\u4e26\u5c07\u5176\u50b3\u905e\u7d66 useMutationEffect \u5373\u53ef\u3002\u51fd\u6578\u5c07\u5728\u6bcf\u6b21\u6e32\u67d3\u5f8c\u57f7\u884c\uff0c\u4e26\u53ef\u4ee5\u7528\u65bc\u8655\u7406\u526f\u4f5c\u7528\u3002<\/p>\n<p>\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u7d44\u4ef6\uff0c\u5b83\u6703\u5728\u6bcf\u6b21\u6e32\u67d3\u5f8c\u767c\u51fa\u4e00\u500b\u8acb\u6c42\uff0c\u4ee5\u7372\u53d6\u6700\u65b0\u7684\u6578\u64da\uff1a<\/p>\n<pre class=\"brush: javascript\">\nimport React, { useMutationEffect } from 'react';\n\nconst MyComponent = () => {\n  useMutationEffect(() => {\n    fetch('\/api\/data')\n      .then(res => res.json())\n      .then(data => {\n        \/\/ do something with the data\n      });\n  });\n\n  return <div>My Component<\/div>;\n};\n\nexport default MyComponent;\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 useMutationEffect \u4f86\u5b9a\u7fa9\u4e00\u500b\u51fd\u6578\uff0c\u8a72\u51fd\u6578\u5c07\u5728\u6bcf\u6b21\u6e32\u67d3\u5f8c\u57f7\u884c\uff0c\u4e26\u767c\u51fa\u4e00\u500b\u8acb\u6c42\uff0c\u4ee5\u7372\u53d6\u6700\u65b0\u7684\u6578\u64da\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>React \u4e2d\u7684 useMutationEffect Hook \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u6bcf\u6b21\u6e32\u67d3\u5f8c\u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7528\u65bc\u8655\u7406\u526f\u4f5c\u7528\u3002\u4f7f\u7528 useMutationEffect \u4f86\u8655\u7406\u526f\u4f5c\u7528\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u5728\u7d44\u4ef6\u4e2d\u5b9a\u7fa9\u4e00\u500b\u51fd\u6578\uff0c\u4e26\u5c07\u5176\u50b3\u905e\u7d66 useMutationEffect \u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1auseMutationEffect\u662fReact\u4e2d\u4e00\u500b\u91cd\u8981\u7684API\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u8655\u7406\u526f\u4f5c\u7528\uff0c\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528useMutationEffect\u4f86\u8655\u7406\u526f\u4f5c\u7528\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-5971","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-1yj","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5971","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=5971"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5971\/revisions"}],"predecessor-version":[{"id":5972,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5971\/revisions\/5972"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}