From 19d0f1c5ed124cda9cc6051cb43ccc252b3938a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=89=A7=E6=AF=85?= Date: Thu, 12 Aug 2021 22:11:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20=E8=A7=A3=E5=86=B3=20?= =?UTF-8?q?Rax=20=E6=A8=A1=E5=BC=8F=E4=B8=8B=E6=8A=A5=E9=94=99=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E4=B8=A2=E5=A4=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/renderer-core/src/renderer/renderer.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/renderer-core/src/renderer/renderer.tsx b/packages/renderer-core/src/renderer/renderer.tsx index e444e7610..3ca21e46a 100644 --- a/packages/renderer-core/src/renderer/renderer.tsx +++ b/packages/renderer-core/src/renderer/renderer.tsx @@ -111,9 +111,17 @@ export default function rendererFactory() { return; } SetComponent.patchedCatch = true; - SetComponent.getDerivedStateFromError = (error: Error) => { - return { engineRenderError: true, error }; + + // Rax 的 getDerivedStateFromError 有 BUG,这里先用 componentDidCatch 来替代 + // @see https://github.com/alibaba/rax/issues/2211 + const originalDidCatch = SetComponent.prototype.componentDidCatch; + SetComponent.prototype.componentDidCatch = function didCatch(this: any, error: Error, errorInfo: any) { + this.setState({ engineRenderError: true, error }); + if (originalDidCatch && typeof originalDidCatch === 'function') { + originalDidCatch.call(this, error, errorInfo); + } }; + const engine = this; const originRender = SetComponent.prototype.render; SetComponent.prototype.render = function () { From 20a7fab565d1a3be2fc9bcd05db2273817bc4fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=89=A7=E6=AF=85?= Date: Fri, 13 Aug 2021 02:24:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20=E8=A7=A3=E5=86=B3=20?= =?UTF-8?q?rax=20v1.2.0=20=E4=B8=AD=E5=9B=A0=E4=B8=BA=20=5F=5FparentInstan?= =?UTF-8?q?ce=20=E8=A2=AB=E5=8E=8B=E7=BC=A9=E6=8E=89=E8=80=8C=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=97=A0=E6=B3=95=E9=80=89=E4=B8=AD=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rax-simulator-renderer/src/renderer.ts | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/rax-simulator-renderer/src/renderer.ts b/packages/rax-simulator-renderer/src/renderer.ts index e27a5e22e..c21051fa2 100644 --- a/packages/rax-simulator-renderer/src/renderer.ts +++ b/packages/rax-simulator-renderer/src/renderer.ts @@ -410,7 +410,9 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer { }; } instance = Instance.get(dom); - while (instance && instance[INTERNAL]) { + + let loopNum = 0; // 防止由于某种意外而导致死循环 + while (instance && instance[INTERNAL] && loopNum < 1000) { if (isValidDesignModeRaxComponentInstance(instance)) { // if (instance && SYMBOL_VNID in instance) { // const docId = (instance.props as any).schema.docId; @@ -422,7 +424,8 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer { }; } - instance = instance[INTERNAL].__parentInstance; + instance = getRaxVDomParentInstance(instance); + loopNum += 1; } return null; @@ -636,4 +639,21 @@ function getLowCodeComponentProps(props: any) { return newProps; } +/** + * 获取 Rax 里面 VDOM 的上一级的实例 + * 注意:Rax 的 development 的包是带有 __parentInstance, + * 但是 production 的包 __parentInstance 会被压缩掉, + * 所以这里遍历下其中的所有值,尝试找到有 _internal 的那个(别的值不会带有这个属性的) + */ +function getRaxVDomParentInstance(instance: { _internal: any }) { + const internalInstance = instance._internal; + return internalInstance.__parentInstance || + Object.values(internalInstance).find(v => ( + v !== null && + v !== instance && + typeof v === 'object' && + typeof (v as {_internal: unknown})._internal === 'object' + )); +} + export default new SimulatorRendererContainer();