Skip to content

Commit 32e0aee

Browse files
committed
perf: cache-component-dom
1 parent 6d3c4b4 commit 32e0aee

File tree

4 files changed

+155
-128
lines changed

4 files changed

+155
-128
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"lint-staged": "^10.2.13",
8989
"react": "16.14.0",
9090
"react-dom": "16.14.0",
91-
"stan-builder": "^0.11.1",
91+
"stan-builder": "^0.11.3",
9292
"standard-version": "^9.0.0",
9393
"ts-jest": "^27.0.3",
9494
"typescript": "4.1.5"

src/components/cache-component-dom/index.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { HTMLAttributes } from 'react';
1+
import React from 'react';
22
import ReactDOM from 'react-dom';
33

44
export type CacheComponentDOMProps = React.PropsWithChildren<{
@@ -20,7 +20,7 @@ export type CacheComponentDOMProps = React.PropsWithChildren<{
2020
/**
2121
* 包裹元素props
2222
*/
23-
containerHTMLProps?: HTMLAttributes<any>;
23+
containerHTMLProps?: React.HTMLAttributes<any>;
2424
}>;
2525

2626
/**
@@ -41,12 +41,13 @@ function CacheComponentDOM(props: CacheComponentDOMProps) {
4141
const [container] = React.useState(getContainer);
4242
const hasUpdate = shouldUpdate();
4343

44+
const divRef = React.useRef(document.createElement('div'));
45+
4446
React.useLayoutEffect(
4547
() => {
4648
moveChildNode(container, elRef.current!);
47-
const div = document.createElement('div');
48-
ReactDOM.render(children as React.ReactElement, div, () => {
49-
elRef.current!.innerHTML = div.innerHTML;
49+
ReactDOM.render(children as React.ReactElement, divRef.current, () => {
50+
elRef.current!.innerHTML = divRef.current.innerHTML;
5051
});
5152
return () => {
5253
container.innerHTML = '';

src/components/make-global-api/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,19 @@ function makeGlobalApi<P = any>(opts: MakeGlobalApiOptions<P>): MakeGlobalApiRet
7070
ReactDOM.render(<Wrapper ref={wrapperRef} />, div);
7171

7272
return {
73-
render(props?: any) {
73+
render(props?: P) {
7474
const index = wrapperRef.current?.add(children, props)!;
7575
return {
7676
index,
7777
unmount() {
7878
return wrapperRef.current?.del(index);
7979
},
80-
update(props: any) {
80+
update(props: P) {
8181
wrapperRef.current?.update(index, props);
8282
},
8383
};
8484
},
85-
update(key, props?: any) {
85+
update(key, props?: P) {
8686
wrapperRef.current?.update(key, props);
8787
},
8888
unmountAll() {

0 commit comments

Comments
 (0)