Skip to content

Commit 9d522f2

Browse files
authored
Merge pull request #1641 from sheinsight/perf/table-prevent-rerender-on-visibility-change
perf(table): prevent unnecessary re-render on visibility change
2 parents a0d4185 + 6175f86 commit 9d522f2

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sheinx",
33
"private": true,
4-
"version": "3.9.10-beta.5",
4+
"version": "3.9.10-beta.6",
55
"description": "A react library developed with sheinx",
66
"module": "dist/index.js",
77
"types": "dist/index.d.ts",

packages/hooks/src/common/use-resize/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const useResize = (props: UseResizeProps) => {
1515
useEffect(() => {
1616
if (!targetRef.current) return;
1717
const el = targetRef.current;
18-
if (el) {
18+
if (el && el.offsetParent !== null) {
1919
setWidth(el.clientWidth);
2020
setHeight(el.clientHeight);
2121
}

packages/hooks/src/utils/dom/element.tsx

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,35 @@ export const addResizeObserver = (
4141
) => {
4242
const { direction, timer } = options;
4343
const [debounceHandler, cleanTimer] = debounce(handler, timer);
44-
let h = debounceHandler;
45-
let lastWidth: number;
46-
let lastHeight: number;
44+
let lastWidth: number = el.clientWidth;
45+
let lastHeight: number = el.clientHeight;
4746
if (window.ResizeObserver) {
48-
if (direction) {
49-
lastWidth = el.clientWidth;
50-
lastHeight = el.clientHeight;
51-
h = (entry: { contentRect: { width: number; height: number } }[]) => {
52-
if (el?.offsetParent === null) {
53-
options.lazy && el.setAttribute('data-observer-hidden', 'true');
54-
return;
55-
}
47+
const h = (entry: { contentRect: { width: number; height: number } }[]) => {
48+
const { width, height } = entry[0].contentRect;
49+
// 元素不可见(display:none)时跳过:offsetParent 为 null 且尺寸为 0
50+
if (el?.offsetParent === null && !width && !height) {
51+
options.lazy && el.setAttribute('data-observer-hidden', 'true');
52+
return;
53+
}
5654

57-
if (options.lazy && el.getAttribute('data-observer-hidden') === 'true') {
58-
el.removeAttribute('data-observer-hidden');
59-
return;
55+
if (options.lazy && el.getAttribute('data-observer-hidden') === 'true') {
56+
el.removeAttribute('data-observer-hidden');
57+
return;
58+
}
59+
if (width && direction === 'x') {
60+
if (lastWidth !== width) {
61+
debounceHandler(entry);
6062
}
61-
const { width, height } = entry[0].contentRect;
62-
if (width && direction === 'x') {
63-
if (lastWidth !== width) {
64-
debounceHandler(entry);
65-
}
66-
} else if (direction === 'y') {
67-
if (height && lastHeight !== height) {
68-
debounceHandler(entry);
69-
}
70-
} else if (lastWidth !== width || lastHeight !== height) {
71-
debounceHandler(entry, { x: lastWidth !== width, y: lastHeight !== height });
63+
} else if (direction === 'y') {
64+
if (height && lastHeight !== height) {
65+
debounceHandler(entry);
7266
}
73-
lastWidth = width;
74-
lastHeight = height;
75-
};
76-
}
67+
} else if (lastWidth !== width || lastHeight !== height) {
68+
debounceHandler(entry, { x: lastWidth !== width, y: lastHeight !== height });
69+
}
70+
lastWidth = width;
71+
lastHeight = height;
72+
};
7773
let observer: ResizeObserver | null = new ResizeObserver(h as ResizeObserverCallback);
7874
observer.observe(el);
7975
return () => {

packages/shineout/src/table/__doc__/changelog.cn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 3.9.10-beta.6
2+
2026-03-04
3+
### 🚀 Performance
4+
- 优化 `Table` 在祖先元素 `display:none` 后恢复显示时不再触发多余的重新渲染 ([#1641](https://github.com/sheinsight/shineout-next/pull/1641))
5+
16
## 3.9.10-beta.2
27
2026-02-28
38
### 💎 Enhancement

0 commit comments

Comments
 (0)