Skip to content

Commit 4ee04f4

Browse files
fix: resolve eslint-plugin-react-hooks 7.0.1 linting errors
Fixed ref access violations introduced by upgrading to eslint-plugin-react-hooks 7.0.1: - Moved ref updates to useEffect in click_outside_wrapper.tsx - Added ESLint disable comments for Floating UI library usage which legitimately requires refs during render - Refactored shadow_root.tsx to use state with queueMicrotask to avoid cascading renders 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 4cbb03a commit 4ee04f4

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

src/click_outside_wrapper.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ const useDetectClickOutside = (
1717
) => {
1818
const ref = useRef<HTMLDivElement | null>(null);
1919
const onClickOutsideRef = useRef(onClickOutside);
20-
onClickOutsideRef.current = onClickOutside;
20+
useEffect(() => {
21+
onClickOutsideRef.current = onClickOutside;
22+
}, [onClickOutside]);
2123
const handleClickOutside = useCallback(
2224
(event: MouseEvent) => {
2325
const target =

src/popper_component.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const PopperComponent: React.FC<PopperComponentProps> = (props) => {
5050
const classes = clsx("react-datepicker-popper", className);
5151
popper = (
5252
<TabLoop enableTabLoop={enableTabLoop}>
53+
{/* eslint-disable react-hooks/refs -- Floating UI values are designed to be used during render */}
5354
<div
5455
ref={popperProps.refs.setFloating}
5556
style={popperProps.floatingStyles}
@@ -71,6 +72,7 @@ export const PopperComponent: React.FC<PopperComponentProps> = (props) => {
7172
/>
7273
)}
7374
</div>
75+
{/* eslint-enable react-hooks/refs */}
7476
</TabLoop>
7577
);
7678
}
@@ -91,6 +93,7 @@ export const PopperComponent: React.FC<PopperComponentProps> = (props) => {
9193

9294
return (
9395
<>
96+
{/* eslint-disable-next-line react-hooks/refs -- Floating UI refs are designed to be used during render */}
9497
<div ref={popperProps.refs.setReference} className={wrapperClasses}>
9598
{targetComponent}
9699
</div>

src/test/helper_components/shadow_root.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,31 @@ import React, {
88
import { createPortal } from "react-dom";
99

1010
const ShadowRoot: FC<PropsWithChildren> = ({ children }) => {
11+
const [shadowRoot, setShadowRoot] = useState<ShadowRoot | null>(null);
12+
1113
const containerRef = useRef<HTMLDivElement>(null);
12-
const shadowRootRef = useRef<ShadowRoot>(null);
13-
const [isInitialized, setIsInitialized] = useState(false);
14+
const isInitializedRef = useRef(false);
1415

1516
useLayoutEffect(() => {
17+
if (isInitializedRef.current) {
18+
return;
19+
}
20+
1621
const container = containerRef.current;
17-
if (isInitialized || !container) {
22+
if (!container) {
1823
return;
1924
}
2025

21-
shadowRootRef.current =
26+
const root =
2227
container.shadowRoot ?? container.attachShadow({ mode: "open" });
23-
setIsInitialized(true);
24-
}, [isInitialized]);
28+
isInitializedRef.current = true;
29+
// Use queueMicrotask to defer setState to avoid cascading renders
30+
queueMicrotask(() => setShadowRoot(root));
31+
}, []);
2532

2633
return (
2734
<div ref={containerRef}>
28-
{isInitialized &&
29-
shadowRootRef.current &&
30-
createPortal(children, shadowRootRef.current)}
35+
{shadowRoot && createPortal(children, shadowRoot)}
3136
</div>
3237
);
3338
};

src/with_floating.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export default function withFloating<T extends FloatingProps>(
5656
middleware: [
5757
flip({ padding: 15 }),
5858
offset(10),
59+
// eslint-disable-next-line react-hooks/refs -- Floating UI requires refs to be passed during render
5960
arrow({ element: arrowRef }),
6061
...(props.popperModifiers ?? []),
6162
],

0 commit comments

Comments
 (0)