-
Notifications
You must be signed in to change notification settings - Fork 0
/
2397-971e7bc28b43d6c8364b.js
1 lines (1 loc) · 42.5 KB
/
2397-971e7bc28b43d6c8364b.js
1
(self.webpackChunkzent_docs=self.webpackChunkzent_docs||[]).push([[2397],{72397:(n,s,a)=>{"use strict";a.r(s),a.d(s,{default:()=>b});var t=a(73450),p=a(27378),o=a(57318),c=a(96681),e=a(14115),l=a(23476),r=a(24246);function u(n,s){var a=Object.keys(n);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(n);s&&(t=t.filter((function(s){return Object.getOwnPropertyDescriptor(n,s).enumerable}))),a.push.apply(a,t)}return a}function i(n){for(var s=1;s<arguments.length;s++){var a=null!=arguments[s]?arguments[s]:{};s%2?u(Object(a),!0).forEach((function(s){(0,t.Z)(n,s,a[s])})):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(a)):u(Object(a)).forEach((function(s){Object.defineProperty(n,s,Object.getOwnPropertyDescriptor(a,s))}))}return n}var k=function(){class n extends p.Component{constructor(...n){super(...n),(0,t.Z)(this,"state",{fail:!1}),(0,t.Z)(this,"triggerFail",(()=>{this.setState({fail:!0})}))}render(){if(this.state.fail)throw new Error("This is an error from render.");return(0,r.jsxs)("div",{children:[(0,r.jsx)("span",{style:{marginRight:10},children:"成功渲染"}),(0,r.jsx)(c.z,{onClick:this.triggerFail,children:"触发错误"})]})}}return(0,r.jsxs)("div",{children:[(0,r.jsx)(e.S,{children:(0,r.jsx)(n,{})}),(0,r.jsx)("div",{style:{marginTop:10},children:"这里不受影响"})]})},d=function(){class n extends p.Component{constructor(...n){super(...n),(0,t.Z)(this,"state",{fail:!1}),(0,t.Z)(this,"triggerFail",(()=>{this.setState({fail:!0})}))}render(){if(this.state.fail)throw new Error("This is an error from render.");return(0,r.jsxs)("div",{children:[(0,r.jsx)("span",{style:{marginRight:10},children:"成功渲染"}),(0,r.jsx)(c.z,{onClick:this.triggerFail,children:"触发错误"})]})}}return(0,r.jsxs)("div",{children:[(0,r.jsx)(e.S,{onError:n=>l.g.error(n.toString()),children:(0,r.jsx)(n,{})}),(0,r.jsx)("div",{style:{marginTop:10},children:"这里不受影响"})]})},m=function(){class n extends p.Component{constructor(...n){super(...n),(0,t.Z)(this,"state",{fail:!1}),(0,t.Z)(this,"triggerFail",(()=>{this.setState({fail:!0})}))}render(){if(this.state.fail)throw new Error("This is an error from render.");return(0,r.jsxs)("div",{children:[(0,r.jsx)("span",{style:{marginRight:10},children:"成功渲染"}),(0,r.jsx)(c.z,{onClick:this.triggerFail,children:"触发错误"})]})}}var s=e.S.withErrorBoundary({Component:n}),a=e.S.catchError()(n);return(0,r.jsxs)("div",{children:[(0,r.jsx)(s,{}),(0,r.jsx)(a,{}),(0,r.jsx)("div",{style:{marginTop:10},children:"这里不受影响"})]})},g=function(){class n extends p.Component{render(){return(0,r.jsx)("div",{children:this.props.error.toString()})}}class s extends p.Component{constructor(...n){super(...n),(0,t.Z)(this,"state",{fail:!1}),(0,t.Z)(this,"triggerFail",(()=>{this.setState({fail:!0})}))}render(){if(this.state.fail)throw new Error("This is an error from render.");return(0,r.jsxs)("div",{children:[(0,r.jsx)("span",{style:{marginRight:10},children:"成功渲染"}),(0,r.jsx)(c.z,{onClick:this.triggerFail,children:"触发错误"})]})}}return(0,r.jsxs)("div",{children:[(0,r.jsx)(e.S,{FallbackComponent:n,children:(0,r.jsx)(s,{})}),(0,r.jsx)("div",{style:{marginTop:10},children:"这里不受影响"})]})};function h(n){return(0,r.jsx)(n.tag,i(i({},n.attributes),{},{dangerouslySetInnerHTML:{__html:n.html}}))}function y(n){return(0,r.jsx)(h,{tag:"section",html:n.html,attributes:{className:"zandoc-react-markdown"}})}function f(n){return(0,r.jsx)(h,{tag:"style",html:n.style})}function v(n,s){for(;n;)return n.offsetTop&&"static"!==getComputedStyle(n).position&&(s+=n.offsetTop),v(n.parentNode,s);return s}class w extends p.Component{constructor(...n){super(...n),(0,t.Z)(this,"state",{showCode:!1}),(0,t.Z)(this,"toggle",(()=>{this.setState({showCode:!this.state.showCode})}))}render(){var n=this.state.showCode,s=this.props,a=s.title,t=s.src,p=s.children;return(0,r.jsxs)("div",{className:"zandoc-react-demo",children:[(0,r.jsx)("div",{className:"zandoc-react-demo__preview",children:p}),(0,r.jsxs)("div",{className:"zandoc-react-demo__bottom",onClick:this.toggle,children:[(0,r.jsx)("div",{className:"zandoc-react-demo__title",children:(0,r.jsx)("p",{children:a||""})}),(0,r.jsx)("i",{className:"zenticon zenticon-caret-up zandoc-react-demo__toggle ".concat(n?"zandoc-react-demo__toggle-on":"zandoc-react-demo__toggle-off")})]}),n&&(0,r.jsx)("pre",{className:"zandoc-react-demo__code",children:(0,r.jsx)(h,{tag:"code",html:t,attributes:{className:"language-jsx"}})})]})}}class b extends p.Component{componentDidMount(){var n=location.hash;if(n){var s=document.querySelector('a[href="'.concat(n,'"]'));s&&(0,o.l)(document.documentElement,0,v(s,-9))}}render(){return p.createElement("div",{className:"zandoc-react-container"},p.createElement(f,{style:""}),p.createElement(y,{html:'<h2 class="anchor-heading"><a href="#errorboundary-cuo-wu-ge-chi">¶</a><a href="javascript:void(0)" id="errorboundary-cuo-wu-ge-chi" class="anchor-point"></a>ErrorBoundary 错误隔离</h2>\n<p>捕获子组件的错误,作用类似 <code>JavaScript</code> 的 <code>try...catch...</code>,但是作用于 <code>React</code> 组件树。</p>\n<h3 class="anchor-heading"><a href="#shi-yong-chang-jing">¶</a><a href="javascript:void(0)" id="shi-yong-chang-jing" class="anchor-point"></a>使用场景</h3>\n<p>用于隔离页面组件的错误。当 <code>ErrorBoundary</code> 子组件中某个组件出错时将错隔离在当前 <code>ErrorBoundary</code> 内,使页面其他部分不受这个错误影响。</p>\n<p>例如,页面分为左右两部分:左侧导航栏,右侧内容区域。可以用 <code>ErrorBoundary</code> 组件分别包裹左右两部分,某一部分出错挂掉的时候不会导致另一部分功能失效。</p>\n<p>注意:</p>\n<ul>\n<li>事件回调函数(例如 <code>onClick</code>)中出现的异常不会被捕获</li>\n<li>异步代码(例如 <code>setTimeout</code>,<code>requestAnimationFrame</code> 等)中的异常也不会被捕获</li>\n<li>需要 <code>React 16</code> 及以上版本支持,老版本中该组件不会生效</li>\n</ul>\n<h3 class="anchor-heading"><a href="#dai-ma-yan-shi">¶</a><a href="javascript:void(0)" id="dai-ma-yan-shi" class="anchor-point"></a>代码演示</h3>'}),p.createElement(w,{title:"基本用法",id:"Demobasic",src:'<span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">ErrorBoundary</span><span class="token punctuation">,</span> <span class="token maybe-class-name">Button</span> <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">\'zent\'</span><span class="token punctuation">;</span>\n\n<span class="token keyword">class</span> <span class="token class-name">ErrorComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>\n state <span class="token operator">=</span> <span class="token punctuation">{</span>\n fail<span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>\n <span class="token punctuation">}</span><span class="token punctuation">;</span>\n\n <span class="token function-variable function">triggerFail</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token method function property-access">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n fail<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>\n <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">;</span>\n\n <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token property-access">state</span><span class="token punctuation">.</span><span class="token property-access">fail</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">\'This is an error from render.\'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span>\n\n <span class="token keyword control-flow">return</span> <span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> marginRight<span class="token operator">:</span> <span class="token number">10</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>成功渲染<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Button</span></span> <span class="token attr-name">onClick</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token property-access">triggerFail</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>触发错误<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Button</span></span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n\n<span class="token maybe-class-name">ReactDOM</span><span class="token punctuation">.</span><span class="token method function property-access">render</span><span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">ErrorBoundary</span></span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">ErrorComponent</span></span> <span class="token punctuation">/></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">ErrorBoundary</span></span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> marginTop<span class="token operator">:</span> <span class="token number">10</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>这里不受影响<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token punctuation">,</span>\n mountNode\n<span class="token punctuation">)</span><span class="token punctuation">;</span>'},p.createElement(k)),p.createElement(w,{title:"onError 回调函数",id:"Demoonerror",src:'<span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">ErrorBoundary</span><span class="token punctuation">,</span> <span class="token maybe-class-name">Button</span><span class="token punctuation">,</span> <span class="token maybe-class-name">Notify</span> <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">\'zent\'</span><span class="token punctuation">;</span>\n\n<span class="token keyword">class</span> <span class="token class-name">ErrorComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>\n state <span class="token operator">=</span> <span class="token punctuation">{</span>\n fail<span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>\n <span class="token punctuation">}</span><span class="token punctuation">;</span>\n\n <span class="token function-variable function">triggerFail</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token method function property-access">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n fail<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>\n <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">;</span>\n\n <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token property-access">state</span><span class="token punctuation">.</span><span class="token property-access">fail</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">\'This is an error from render.\'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span>\n\n <span class="token keyword control-flow">return</span> <span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> marginRight<span class="token operator">:</span> <span class="token number">10</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>成功渲染<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Button</span></span> <span class="token attr-name">onClick</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token property-access">triggerFail</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>触发错误<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Button</span></span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n\n<span class="token maybe-class-name">ReactDOM</span><span class="token punctuation">.</span><span class="token method function property-access">render</span><span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">ErrorBoundary</span></span> <span class="token attr-name">onError</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token parameter">err</span> <span class="token arrow operator">=></span> <span class="token maybe-class-name">Notify</span><span class="token punctuation">.</span><span class="token method function property-access">error</span><span class="token punctuation">(</span>err<span class="token punctuation">.</span><span class="token method function property-access">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">ErrorComponent</span></span> <span class="token punctuation">/></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">ErrorBoundary</span></span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> marginTop<span class="token operator">:</span> <span class="token number">10</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>这里不受影响<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token punctuation">,</span>\n mountNode\n<span class="token punctuation">)</span><span class="token punctuation">;</span>'},p.createElement(d)),p.createElement(w,{title:"withErrorBoundary 以及 catchError 高阶组件",id:"Demohoc",src:'<span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">ErrorBoundary</span><span class="token punctuation">,</span> <span class="token maybe-class-name">Button</span> <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">\'zent\'</span><span class="token punctuation">;</span>\n\n<span class="token keyword">class</span> <span class="token class-name">ErrorComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>\n state <span class="token operator">=</span> <span class="token punctuation">{</span>\n fail<span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>\n <span class="token punctuation">}</span><span class="token punctuation">;</span>\n\n <span class="token function-variable function">triggerFail</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token method function property-access">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n fail<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>\n <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">;</span>\n\n <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token property-access">state</span><span class="token punctuation">.</span><span class="token property-access">fail</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">\'This is an error from render.\'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span>\n\n <span class="token keyword control-flow">return</span> <span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> marginRight<span class="token operator">:</span> <span class="token number">10</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>成功渲染<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Button</span></span> <span class="token attr-name">onClick</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token property-access">triggerFail</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>触发错误<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Button</span></span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n\n<span class="token keyword">const</span> <span class="token maybe-class-name">GuardedErrorComponent</span> <span class="token operator">=</span> <span class="token maybe-class-name">ErrorBoundary</span><span class="token punctuation">.</span><span class="token method function property-access">withErrorBoundary</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n <span class="token maybe-class-name">Component</span><span class="token operator">:</span> <span class="token maybe-class-name">ErrorComponent</span><span class="token punctuation">,</span>\n<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token keyword">const</span> <span class="token maybe-class-name">GuardedErrorComponent2</span> <span class="token operator">=</span> <span class="token maybe-class-name">ErrorBoundary</span><span class="token punctuation">.</span><span class="token method function property-access">catchError</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token maybe-class-name">ErrorComponent</span><span class="token punctuation">)</span>\n\n<span class="token maybe-class-name">ReactDOM</span><span class="token punctuation">.</span><span class="token method function property-access">render</span><span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">GuardedErrorComponent</span></span> <span class="token punctuation">/></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">GuardedErrorComponent2</span></span> <span class="token punctuation">/></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> marginTop<span class="token operator">:</span> <span class="token number">10</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>这里不受影响<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token punctuation">,</span>\n mountNode\n<span class="token punctuation">)</span><span class="token punctuation">;</span>'},p.createElement(m)),p.createElement(w,{title:"自定义 FallbackComponent",id:"Democustom",src:'<span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">ErrorBoundary</span><span class="token punctuation">,</span> <span class="token maybe-class-name">Button</span> <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">\'zent\'</span><span class="token punctuation">;</span>\n\n<span class="token keyword">class</span> <span class="token class-name">Fallback</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>\n <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">return</span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token property-access">props</span><span class="token punctuation">.</span><span class="token property-access">error</span><span class="token punctuation">.</span><span class="token method function property-access">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n\n<span class="token keyword">class</span> <span class="token class-name">ErrorComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>\n state <span class="token operator">=</span> <span class="token punctuation">{</span>\n fail<span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>\n <span class="token punctuation">}</span><span class="token punctuation">;</span>\n\n <span class="token function-variable function">triggerFail</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token method function property-access">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n fail<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>\n <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">;</span>\n\n <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token property-access">state</span><span class="token punctuation">.</span><span class="token property-access">fail</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">\'This is an error from render.\'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span>\n\n <span class="token keyword control-flow">return</span> <span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> marginRight<span class="token operator">:</span> <span class="token number">10</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>成功渲染<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Button</span></span> <span class="token attr-name">onClick</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token property-access">triggerFail</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>触发错误<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Button</span></span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n\n<span class="token maybe-class-name">ReactDOM</span><span class="token punctuation">.</span><span class="token method function property-access">render</span><span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">ErrorBoundary</span></span> <span class="token attr-name">FallbackComponent</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token maybe-class-name">Fallback</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">ErrorComponent</span></span> <span class="token punctuation">/></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">ErrorBoundary</span></span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> marginTop<span class="token operator">:</span> <span class="token number">10</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>这里不受影响<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token punctuation">,</span>\n mountNode\n<span class="token punctuation">)</span><span class="token punctuation">;</span>'},p.createElement(g)),p.createElement(y,{html:'<h3 class="anchor-heading"><a href="#api">¶</a><a href="javascript:void(0)" id="api" class="anchor-point"></a>API</h3>\n<table class="table">\n<thead>\n<tr>\n<th>参数</th>\n<th>说明</th>\n<th>类型</th>\n<th>是否必须</th>\n<th>默认值</th>\n<th>备选值</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>children</td>\n<td>受保护的子组件</td>\n<td><code>node</code></td>\n<td>是</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td>onError</td>\n<td>子组件出错时的回调函数</td>\n<td><code>(error: Error, componentStack: string): void</code></td>\n<td>否</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td>FallbackComponent</td>\n<td>出错时用来替换子组件的组件</td>\n<td><code>Component</code></td>\n<td>否</td>\n<td></td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<p><code>FallbackComponent</code> 接受两个参数: <code>error</code> 以及 <code>componentStack</code>,和 <code>onError</code> 一致。</p>\n<h4 class="anchor-heading"><a href="#witherrorboundary">¶</a><a href="javascript:void(0)" id="witherrorboundary" class="anchor-point"></a>withErrorBoundary</h4>\n<p>这个高阶组件可以用来装饰组件,装饰后的组件会自动隔离自身以及子组件的错误。函数原型:</p>\n<pre><code class="language-ts"><span class="token function">withErrorBoundary</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n BaseComponent<span class="token operator">:</span> React<span class="token punctuation">.</span>Component<span class="token punctuation">,</span>\n onError<span class="token operator">?</span><span class="token operator">:</span> <span class="token punctuation">(</span>error<span class="token operator">:</span> Error<span class="token punctuation">,</span> stackTrace<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span><span class="token punctuation">,</span>\n FallbackComponent<span class="token operator">?</span><span class="token operator">:</span> React<span class="token punctuation">.</span>Component\n<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">:</span> React<span class="token punctuation">.</span>Component</code></pre>\n<h4 class="anchor-heading"><a href="#catcherror">¶</a><a href="javascript:void(0)" id="catcherror" class="anchor-point"></a>catchError</h4>\n<p>这个高阶组件更适合用在使用 decorator 的场景,功能和 withErrorBoundary 一致。函数原型:</p>\n<pre><code class="language-ts"><span class="token function">catchError</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n onError<span class="token operator">?</span><span class="token operator">:</span> <span class="token punctuation">(</span>error<span class="token operator">:</span> Error<span class="token punctuation">,</span> stackTrace<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span><span class="token punctuation">,</span>\n FallbackComponent<span class="token operator">?</span><span class="token operator">:</span> React<span class="token punctuation">.</span>Component\n<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>BaseComponent<span class="token operator">:</span> React<span class="token punctuation">.</span>Component<span class="token punctuation">)</span><span class="token operator">:</span> React<span class="token punctuation">.</span>Component<span class="token punctuation">)</span></code></pre>'}))}}},14115:(n,s,a)=>{"use strict";a.d(s,{S:()=>d,Z:()=>m});var t=a(59312),p=a(24246),o=a(27378),c=a(27036),e=a(85883),l={whiteSpace:"pre"};function r(n){var s=n.componentStack,a=n.error;return(0,p.jsx)("div",(0,t.pi)({className:"zent-error-boundary-fallback-component","data-zv":"9.12.16"},{children:(0,p.jsxs)("div",(0,t.pi)({className:"zent-error-boundary-fallback-component__error","data-zv":"9.12.16"},{children:[(0,p.jsx)(e.Z,(0,t.pi)({trigger:"hover",position:"auto-bottom-center",centerArrow:!0,content:(0,p.jsx)("div",(0,t.pi)({style:l,"data-zv":"9.12.16"},{children:s}),void 0)},{children:(0,p.jsx)(c.Z,{type:"error-circle"},void 0)}),void 0),(0,p.jsx)("span",(0,t.pi)({"data-zv":"9.12.16"},{children:a.toString()}),void 0)]}),void 0)}),void 0)}var u=function(n){var s=void 0===n?{}:n,a=s.FallbackComponent,o=s.onError;return function(n){return function(s){return(0,p.jsx)(m,(0,t.pi)({FallbackComponent:a,onError:o},{children:(0,p.jsx)(n,(0,t.pi)({},s),void 0)}),void 0)}}},i=function(n){var s=void 0===n?{}:n,a=s.Component,o=s.FallbackComponent,c=s.onError;return function(n){return(0,p.jsx)(m,(0,t.pi)({FallbackComponent:o,onError:c},{children:(0,p.jsx)(a,(0,t.pi)({},n),void 0)}),void 0)}};function k(n){return n?n.componentStack:""}var d=function(n){function s(){var s=null!==n&&n.apply(this,arguments)||this;return s.state={error:null,info:null},s}return(0,t.ZT)(s,n),s.prototype.componentDidCatch=function(n,s){var a=this.props.onError;if("function"==typeof a)try{a(n,k(s))}catch(n){}this.setState({error:n,info:s})},s.prototype.render=function(){var n=this.props,s=n.children,a=n.FallbackComponent,t=this.state,o=t.error,c=t.info;return null!==o?(0,p.jsx)(a,{componentStack:k(c),error:o},void 0):s},s.defaultProps={FallbackComponent:r},s.withErrorBoundary=i,s.catchError=u,s}(o.Component),m=d},85883:(n,s,a)=>{"use strict";var t=a(92497);s.Z=t.Z}}]);