{"id":5853,"date":"2023-01-06T15:17:12","date_gmt":"2023-01-06T07:17:12","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5853"},"modified":"2023-01-06T15:17:12","modified_gmt":"2023-01-06T07:17:12","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%e8%99%95%e7%90%86%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%e8%99%95%e7%90%86%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528\u932f\u8aa4\u8655\u7406\uff1f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, Error Handling\"><\/p>\n<h1>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u932f\u8aa4\u8655\u7406(error handling)\uff1f<\/h1>\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\u7528\u6236\u754c\u9762\u3002\u7136\u800c\uff0c\u5728\u958b\u767c React \u61c9\u7528\u7a0b\u5e8f\u6642\uff0c\u932f\u8aa4\u8655\u7406\u662f\u4e00\u500b\u91cd\u8981\u7684\u8003\u616e\u56e0\u7d20\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u8a0e\u8ad6\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u932f\u8aa4\u8655\u7406\u3002<\/p>\n<h2>\u4f7f\u7528 try\/catch \u8a9e\u53e5<\/h2>\n<p>\u6700\u5e38\u898b\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 try\/catch \u8a9e\u53e5\u4f86\u6355\u7372\u932f\u8aa4\u3002\u5728 React \u4e2d\uff0c\u53ef\u4ee5\u5728 render() \u65b9\u6cd5\u4e2d\u4f7f\u7528 try\/catch \u8a9e\u53e5\u4f86\u6355\u7372\u932f\u8aa4\uff0c\u4e26\u5c07\u932f\u8aa4\u8a0a\u606f\u986f\u793a\u7d66\u7528\u6236\u3002<\/p>\n<pre class=\"brush: javascript\">\ntry {\n  \/\/ \u9019\u88e1\u662f\u53ef\u80fd\u51fa\u932f\u7684\u7a0b\u5f0f\u78bc\n} catch (error) {\n  \/\/ \u9019\u88e1\u662f\u6355\u7372\u932f\u8aa4\u7684\u7a0b\u5f0f\u78bc\n  return {error.message}<\/p>;\n}\n<\/pre>\n<h2>\u4f7f\u7528 ErrorBoundary \u7d44\u4ef6<\/h2>\n<p>ErrorBoundary \u7d44\u4ef6\u662f React 16 \u4e2d\u65b0\u589e\u7684\u4e00\u500b\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u6355\u7372\u5b50\u7d44\u4ef6\u7684\u932f\u8aa4\uff0c\u4e26\u5c07\u932f\u8aa4\u8a0a\u606f\u986f\u793a\u7d66\u7528\u6236\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    \/\/ \u66f4\u65b0 state \u4f7f\u4e0b\u4e00\u6b21 render \u6703\u986f\u793a\u932f\u8aa4\u8a0a\u606f\n    return { hasError: true };\n  }\n\n  componentDidCatch(error, errorInfo) {\n    \/\/ \u4f60\u4e5f\u53ef\u4ee5\u5c07\u932f\u8aa4\u8a0a\u606f\u4e0a\u50b3\u5230\u4e00\u500b\u932f\u8aa4\u5831\u544a\u670d\u52d9\n    logErrorToMyService(error, errorInfo);\n  }\n\n  render() {\n    if (this.state.hasError) {\n      \/\/ \u4f60\u53ef\u4ee5\u81ea\u8a02\u932f\u8aa4\u8a0a\u606f\n      return <h1>Something went wrong.<\/h1>;\n    }\n\n    return this.props.children; \n  }\n}\n<\/pre>\n<h2>\u4f7f\u7528 Error \u7269\u4ef6<\/h2>\n<p>\u53e6\u4e00\u7a2e\u65b9\u6cd5\u662f\u4f7f\u7528 Error \u7269\u4ef6\u4f86\u6355\u7372\u932f\u8aa4\uff0c\u4e26\u5c07\u932f\u8aa4\u8a0a\u606f\u986f\u793a\u7d66\u7528\u6236\u3002<\/p>\n<pre class=\"brush: javascript\">\nclass MyComponent extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = { error: null };\n  }\n\n  componentDidCatch(error, errorInfo) {\n    \/\/ \u66f4\u65b0 state \u4f7f\u4e0b\u4e00\u6b21 render \u6703\u986f\u793a\u932f\u8aa4\u8a0a\u606f\n    this.setState({\n      error: error\n    });\n  }\n\n  render() {\n    if (this.state.error) {\n      \/\/ \u4f60\u53ef\u4ee5\u81ea\u8a02\u932f\u8aa4\u8a0a\u606f\n      return <h1>{this.state.error.message}<\/h1>;\n    }\n\n    return this.props.children; \n  }\n}\n<\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728 React \u4e2d\u4f7f\u7528\u932f\u8aa4\u8655\u7406\u662f\u4e00\u500b\u91cd\u8981\u7684\u8003\u616e\u56e0\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 try\/catch \u8a9e\u53e5\u3001ErrorBoundary \u7d44\u4ef6\u548c Error \u7269\u4ef6\u4f86\u6355\u7372\u932f\u8aa4\uff0c\u4e26\u5c07\u932f\u8aa4\u8a0a\u606f\u986f\u793a\u7d66\u7528\u6236\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\u8655\u7406\uff0c\u5f9e\u6355\u7372\u932f\u8aa4\u5230\u8655\u7406\u932f\u8aa4\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528React\u63d0\u4f9b\u7684API\u4f86\u66f4\u6709\u6548\u5730\u8655\u7406\u932f\u8aa4\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-5853","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-1wp","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5853","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=5853"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5853\/revisions"}],"predecessor-version":[{"id":5854,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5853\/revisions\/5854"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}