Skip to content

Commit

Permalink
fix: hoc 组件缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
rainke committed Oct 17, 2023
1 parent d4822af commit 76557f4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/renderer-core/src/hoc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ function patchDidCatch(Comp: any, { baseRenderer }: Options) {
}
}

const cache = new Map();

export function compWrapper(Comp: any, options: Options) {
const { createElement, Component, forwardRef } = adapter.getRuntime();
if (
Expand All @@ -72,6 +74,11 @@ export function compWrapper(Comp: any, options: Options) {
patchDidCatch(Comp, options);
return Comp;
}

if (cache.has(options.schema.id)) {
return cache.get(options.schema.id);
}

class Wrapper extends Component {
render() {
return createElement(Comp, { ...this.props, ref: this.props.forwardRef });
Expand All @@ -82,10 +89,14 @@ export function compWrapper(Comp: any, options: Options) {
patchDidCatch(Wrapper, options);
patchDidMount(Wrapper, options);

return cloneEnumerableProperty(
const WrapperComponent = cloneEnumerableProperty(
forwardRef((props: any, ref: any) => {
return createElement(Wrapper, { ...props, forwardRef: ref });
}),
Comp,
);

cache.set(options.schema.id, WrapperComponent);

return WrapperComponent;
}

0 comments on commit 76557f4

Please sign in to comment.