{"id":5907,"date":"2023-01-06T15:32:58","date_gmt":"2023-01-06T07:32:58","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5907"},"modified":"2023-01-06T15:32:58","modified_gmt":"2023-01-06T07:32:58","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8%e9%8c%af%e8%aa%a4%e9%82%8a%e7%95%8cerrorboundaries%e8%99%95%e7%90%86%e7%b5%84%e4%bb%b6%e5%85%a7%e7%9a%84%e7%95%b0%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8%e9%8c%af%e8%aa%a4%e9%82%8a%e7%95%8cerrorboundaries%e8%99%95%e7%90%86%e7%b5%84%e4%bb%b6%e5%85%a7%e7%9a%84%e7%95%b0%e5%b8%b8%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528\u932f\u8aa4\u908a\u754c(ErrorBoundaries)\u8655\u7406\u7d44\u4ef6\u5167\u7684\u7570\u5e38\uff1f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, Error Boundaries, \u9519\u8bef\u8fb9\u754c, \u7ec4\u4ef6\u5185\u7684\u5f02\u5e38\"><\/p>\n<h1>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u9519\u8bef\u8fb9\u754c(Error Boundaries)\u5904\u7406\u7ec4\u4ef6\u5185\u7684\u5f02\u5e38\uff1f<\/h1>\n<p>React \u63d0\u4f9b\u4e86\u4e00\u79cd\u53eb\u505a\u9519\u8bef\u8fb9\u754c(Error Boundaries)\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u7ec4\u4ef6\u5185\u7684\u5f02\u5e38\u3002\u9519\u8bef\u8fb9\u754c\u662f\u4e00\u79cd\u7279\u6b8a\u7684 React \u7ec4\u4ef6\uff0c\u5b83\u53ef\u4ee5\u6355\u83b7\u5728\u5176\u5b50\u7ec4\u4ef6\u6811\u4e2d\u629b\u51fa\u7684 JavaScript \u9519\u8bef\uff0c\u5e76\u4e14\u53ef\u4ee5\u6e32\u67d3\u51fa\u5907\u7528 UI\uff0c\u800c\u4e0d\u662f\u5d29\u6e83\u7684\u7ec4\u4ef6\u6811\u3002<\/p>\n<p>\u4f7f\u7528\u9519\u8bef\u8fb9\u754c\u53ef\u4ee5\u8ba9\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\uff0c\u800c\u4e0d\u662f\u5d29\u6e83\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6355\u83b7\u7ec4\u4ef6\u5185\u7684\u5f02\u5e38\uff0c\u5e76\u4e14\u53ef\u4ee5\u6e32\u67d3\u51fa\u5907\u7528 UI\uff0c\u800c\u4e0d\u662f\u5d29\u6e83\u7684\u7ec4\u4ef6\u6811\u3002<\/p>\n<h2>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u9519\u8bef\u8fb9\u754c\uff1f<\/h2>\n<p>\u8981\u5728 React \u4e2d\u4f7f\u7528\u9519\u8bef\u8fb9\u754c\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u5b83\u53ef\u4ee5\u6355\u83b7 JavaScript \u9519\u8bef\uff0c\u5e76\u4e14\u53ef\u4ee5\u6e32\u67d3\u51fa\u5907\u7528 UI\uff0c\u800c\u4e0d\u662f\u5d29\u6e83\u7684\u7ec4\u4ef6\u6811\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 React \u7684 <code>componentDidCatch()<\/code> \u751f\u547d\u5468\u671f\u65b9\u6cd5\u6765\u5b9e\u73b0\u9519\u8bef\u8fb9\u754c\uff0c\u5b83\u53ef\u4ee5\u6355\u83b7\u7ec4\u4ef6\u5185\u7684\u5f02\u5e38\uff0c\u5e76\u4e14\u53ef\u4ee5\u6e32\u67d3\u51fa\u5907\u7528 UI\uff0c\u800c\u4e0d\u662f\u5d29\u6e83\u7684\u7ec4\u4ef6\u6811\u3002<\/p>\n<pre class=\"brush: javascript\">\nclass ErrorBoundary extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = { hasError: false };\n  }\n\n  static getDerivedStateFromError(error) {\n    \/\/ Update state so the next render will show the fallback UI.\n    return { hasError: true };\n  }\n\n  componentDidCatch(error, info) {\n    \/\/ You can also log the error to an error reporting service\n    logErrorToMyService(error, info);\n  }\n\n  render() {\n    if (this.state.hasError) {\n      \/\/ You can render any custom fallback UI\n      return <h1>Something went wrong.<\/h1>;\n    }\n\n    return this.props.children; \n  }\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>ErrorBoundary<\/code> \u7ec4\u4ef6\uff0c\u5b83\u53ef\u4ee5\u6355\u83b7\u7ec4\u4ef6\u5185\u7684\u5f02\u5e38\uff0c\u5e76\u4e14\u53ef\u4ee5\u6e32\u67d3\u51fa\u5907\u7528 UI\uff0c\u800c\u4e0d\u662f\u5d29\u6e83\u7684\u7ec4\u4ef6\u6811\u3002<\/p>\n<p>\u8981\u4f7f\u7528\u9519\u8bef\u8fb9\u754c\uff0c\u4f60\u53ea\u9700\u8981\u5c06\u5b83\u5305\u88f9\u5728\u4f60\u60f3\u8981\u6355\u83b7\u5f02\u5e38\u7684\u7ec4\u4ef6\u5916\u9762\uff1a<\/p>\n<pre class=\"brush: javascript\">\n<ErrorBoundary>\n  <MyWidget \/>\n<\/ErrorBoundary>\n<\/pre>\n<p>\u8fd9\u6837\uff0c\u5982\u679c <code>MyWidget<\/code> \u7ec4\u4ef6\u5185\u53d1\u751f\u4e86\u5f02\u5e38\uff0c<code>ErrorBoundary<\/code> \u7ec4\u4ef6\u5c31\u4f1a\u6355\u83b7\u5b83\uff0c\u5e76\u4e14\u53ef\u4ee5\u6e32\u67d3\u51fa\u5907\u7528 UI\uff0c\u800c\u4e0d\u662f\u5d29\u6e83\u7684\u7ec4\u4ef6\u6811\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>React \u63d0\u4f9b\u4e86\u4e00\u79cd\u53eb\u505a\u9519\u8bef\u8fb9\u754c(Error Boundaries)\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u7ec4\u4ef6\u5185\u7684\u5f02\u5e38\u3002\u9519\u8bef\u8fb9\u754c\u662f\u4e00\u79cd\u7279\u6b8a\u7684 React \u7ec4\u4ef6\uff0c\u5b83\u53ef\u4ee5\u6355\u83b7\u5728\u5176\u5b50\u7ec4\u4ef6\u6811\u4e2d\u629b\u51fa\u7684 JavaScript \u9519\u8bef\uff0c\u5e76\u4e14\u53ef\u4ee5\u6e32\u67d3\u51fa\u5907\u7528 UI\uff0c\u800c\u4e0d\u662f\u5d29\u6e83\u7684\u7ec4\u4ef6\u6811\u3002\u8981\u5728 React \u4e2d\u4f7f\u7528\u9519\u8bef\u8fb9\u754c\uff0c\u4f60\u53ea\u9700\u8981\u5c06\u5b83\u5305\u88f9\u5728\u4f60\u60f3\u8981\u6355\u83b7\u5f02\u5e38\u7684\u7ec4\u4ef6\u5916\u9762\uff0c\u8fd9\u6837\uff0c\u5982\u679c\u7ec4\u4ef6\u5185\u53d1\u751f\u4e86\u5f02\u5e38\uff0c\u9519\u8bef\u8fb9\u754c\u7ec4\u4ef6\u5c31\u4f1a\u6355\u83b7\u5b83\uff0c\u5e76\u4e14\u53ef\u4ee5\u6e32\u67d3\u51fa\u5907\u7528 UI\uff0c\u800c\u4e0d\u662f\u5d29\u6e83\u7684\u7ec4\u4ef6\u6811\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528\u932f\u8aa4\u908a\u754c(ErrorBoundaries)\u8655\u7406\u7d44\u4ef6\u5167\u7684\u7570\u5e38\uff0c\u5f9e\u5b9a\u7fa9\u932f\u8aa4\u908a\u754c\u5230\u6355\u7372\u548c\u8655\u7406\u932f\u8aa4\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u932f\u8aa4\u908a\u754c\u4f86\u4fdd\u8b77\u61c9\u7528\u7a0b\u5e8f\u514d\u53d7\u5d29\u6f70\u7684\u5f71\u97ff\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-5907","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-1xh","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5907","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=5907"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5907\/revisions"}],"predecessor-version":[{"id":5908,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5907\/revisions\/5908"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}