From 59f3ce5b72c5ee46c51dee6199fa1380fa3867ae Mon Sep 17 00:00:00 2001 From: "liujuping.liujupin" Date: Thu, 9 Dec 2021 14:42:39 +0800 Subject: [PATCH] fix: cache pre props. to fix cant keep set value --- packages/renderer-core/src/hoc/leaf.tsx | 32 +++++++++++++------------ 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/renderer-core/src/hoc/leaf.tsx b/packages/renderer-core/src/hoc/leaf.tsx index 979e09e34..abc92acd3 100644 --- a/packages/renderer-core/src/hoc/leaf.tsx +++ b/packages/renderer-core/src/hoc/leaf.tsx @@ -218,6 +218,8 @@ export function leafWrapper(Comp: types.IBaseRenderer, { nodeChildren: null, childrenInState: false, visible: !hidden, + nodeCacheProps: {}, + nodeProps: {}, }; } @@ -349,11 +351,11 @@ export function leafWrapper(Comp: types.IBaseRenderer, { this.curEventLeaf = _leaf; } - this.setState({ - nodeChildren: null, - nodeProps: {}, - childrenInState: false, - }); + const { + visible, + ...resetState + } = this.defaultState; + this.setState(resetState); } /** 监听参数变化 */ @@ -378,23 +380,22 @@ export function leafWrapper(Comp: types.IBaseRenderer, { return; } this.beforeRender(RerenderType.PropsChanged); + const state = this.state; + const nodeCacheProps = state.nodeCacheProps; const nodeProps = getProps(node?.export?.(TransformStage.Render) as types.ISchema, scope, Comp, componentInfo); - const preNodeProps = this.state.nodeProps; - const newNodeProps = { - ...preNodeProps, - ...nodeProps, - }; - if (key && !(key in newNodeProps) && (key in this.props)) { + if (key && !(key in nodeProps) && (key in this.props)) { // 当 key 在 this.props 中时,且不存在在计算值中,需要用 newValue 覆盖掉 this.props 的取值 - newNodeProps[key] = newValue; + nodeCacheProps[key] = newValue; } - __debug(`${leaf?.componentName}[${this.props.componentId}] component trigger onPropsChange!`, newNodeProps); + __debug(`${leaf?.componentName}[${this.props.componentId}] component trigger onPropsChange!`, nodeProps, nodeCacheProps, key, newValue); this.setState('children' in nodeProps ? { nodeChildren: nodeProps.children, - nodeProps: newNodeProps, + nodeProps, childrenInState: true, + nodeCacheProps, } : { - nodeProps: newNodeProps, + nodeProps, + nodeCacheProps, }); }); @@ -497,6 +498,7 @@ export function leafWrapper(Comp: types.IBaseRenderer, { const compProps = { ...rest, + ...(this.state.nodeCacheProps || {}), ...(this.state.nodeProps || {}), children: [], __id: this.props.componentId,