Skip to content

Commit 387ee23

Browse files
authored
Merge pull request #501 from cornelius-behrend/ResizeObserver-loop-limit-exceeded
fix: ResizeObserver loop limit exceeded
2 parents 9f116c6 + fd0a4b9 commit 387ee23

File tree

1 file changed

+63
-66
lines changed

1 file changed

+63
-66
lines changed

src/index.ts

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -107,83 +107,80 @@ const useDimensions = <T extends HTMLElement | null>({
107107
// eslint-disable-next-line compat/compat
108108
observerRef.current = new (polyfill || window.ResizeObserver)(
109109
([entry]: any) => {
110-
const { contentBoxSize, borderBoxSize, contentRect } = entry;
111-
112-
let boxSize = contentBoxSize;
113-
if (useBorderBoxSize)
114-
if (borderBoxSize) {
115-
boxSize = borderBoxSize;
116-
} else if (!warnedRef.current) {
117-
console.warn(borderBoxWarn);
118-
warnedRef.current = true;
110+
rafId = window.requestAnimationFrame(() => {
111+
const { contentBoxSize, borderBoxSize, contentRect } = entry;
112+
113+
let boxSize = contentBoxSize;
114+
if (useBorderBoxSize)
115+
if (borderBoxSize) {
116+
boxSize = borderBoxSize;
117+
} else if (!warnedRef.current) {
118+
console.warn(borderBoxWarn);
119+
warnedRef.current = true;
120+
}
121+
// @juggle/resize-observer polyfill has different data structure
122+
boxSize = Array.isArray(boxSize) ? boxSize[0] : boxSize;
123+
124+
const width = boxSize ? boxSize.inlineSize : contentRect.width;
125+
const height = boxSize ? boxSize.blockSize : contentRect.height;
126+
127+
if (
128+
width === prevSizeRef.current.width &&
129+
height === prevSizeRef.current.height
130+
)
131+
return;
132+
133+
prevSizeRef.current = { width, height };
134+
135+
const e = {
136+
currentBreakpoint: "",
137+
width,
138+
height,
139+
entry,
140+
observe,
141+
unobserve,
142+
};
143+
144+
if (breakpoints) {
145+
e.currentBreakpoint = getCurrentBreakpoint(breakpoints, width);
146+
147+
if (e.currentBreakpoint !== prevBreakpointRef.current) {
148+
if (onResizeRef.current) onResizeRef.current(e);
149+
prevBreakpointRef.current = e.currentBreakpoint;
150+
}
151+
} else if (onResizeRef.current) {
152+
onResizeRef.current(e);
119153
}
120-
// @juggle/resize-observer polyfill has different data structure
121-
boxSize = Array.isArray(boxSize) ? boxSize[0] : boxSize;
122-
123-
const width = boxSize ? boxSize.inlineSize : contentRect.width;
124-
const height = boxSize ? boxSize.blockSize : contentRect.height;
125-
126-
if (
127-
width === prevSizeRef.current.width &&
128-
height === prevSizeRef.current.height
129-
)
130-
return;
131-
132-
prevSizeRef.current = { width, height };
133-
134-
const e = {
135-
currentBreakpoint: "",
136-
width,
137-
height,
138-
entry,
139-
observe,
140-
unobserve,
141-
};
142-
143-
if (breakpoints) {
144-
e.currentBreakpoint = getCurrentBreakpoint(breakpoints, width);
145-
146-
if (e.currentBreakpoint !== prevBreakpointRef.current) {
147-
if (onResizeRef.current) onResizeRef.current(e);
148-
prevBreakpointRef.current = e.currentBreakpoint;
149-
}
150-
} else if (onResizeRef.current) {
151-
onResizeRef.current(e);
152-
}
153-
154-
const next = {
155-
currentBreakpoint: e.currentBreakpoint,
156-
width,
157-
height,
158-
entry,
159-
};
160-
161-
if (shouldUpdateRef.current && !shouldUpdateRef.current(next)) return;
162-
163-
if (
164-
!shouldUpdateRef.current &&
165-
breakpoints &&
166-
updateOnBreakpointChange
167-
) {
168-
rafId = requestAnimationFrame(() => {
154+
155+
const next = {
156+
currentBreakpoint: e.currentBreakpoint,
157+
width,
158+
height,
159+
entry,
160+
};
161+
162+
if (shouldUpdateRef.current && !shouldUpdateRef.current(next)) return;
163+
164+
if (
165+
!shouldUpdateRef.current &&
166+
breakpoints &&
167+
updateOnBreakpointChange
168+
) {
169169
setState((prev) =>
170170
prev.currentBreakpoint !== next.currentBreakpoint ? next : prev
171171
);
172-
});
173-
return;
174-
}
172+
return;
173+
}
175174

176-
rafId = requestAnimationFrame(() => {
177175
setState(next);
178-
});
179-
}
180-
);
176+
})
177+
});
181178

182179
observe();
183180

184181
return () => {
185182
unobserve();
186-
if (rafId) cancelAnimationFrame(rafId);
183+
if (rafId) window.cancelAnimationFrame(rafId);
187184
};
188185
// eslint-disable-next-line react-hooks/exhaustive-deps
189186
}, [

0 commit comments

Comments
 (0)