Skip to content

Commit 8ca4953

Browse files
fix: useEffect() infinite loop
1 parent f6d21c7 commit 8ca4953

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

src/components/TooltipProvider/TooltipProvider.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,32 @@ import React, {
77
useState,
88
} from 'react'
99

10-
import type { AnchorRef, TooltipContextData } from './TooltipProviderTypes'
10+
import type {
11+
AnchorRef,
12+
TooltipContextData,
13+
TooltipContextDataWrapper,
14+
} from './TooltipProviderTypes'
1115

1216
const DEFAULT_TOOLTIP_ID = 'DEFAULT_TOOLTIP_ID'
17+
const DEFAULT_CONTEXT_DATA: TooltipContextData = {
18+
anchorRefs: new Set(),
19+
activeAnchor: { current: null },
20+
attach: () => {
21+
/* attach anchor element */
22+
},
23+
detach: () => {
24+
/* detach anchor element */
25+
},
26+
setActiveAnchor: () => {
27+
/* set active anchor */
28+
},
29+
}
1330

14-
const defaultContextData: TooltipContextData = {
15-
getTooltipData: () => ({
16-
anchorRefs: new Set(),
17-
activeAnchor: { current: null },
18-
attach: () => {
19-
/* attach anchor element */
20-
},
21-
detach: () => {
22-
/* detach anchor element */
23-
},
24-
setActiveAnchor: () => {
25-
/* set active anchor */
26-
},
27-
}),
31+
const DEFAULT_CONTEXT_DATA_WRAPPER: TooltipContextDataWrapper = {
32+
getTooltipData: () => DEFAULT_CONTEXT_DATA,
2833
}
2934

30-
const TooltipContext = createContext<TooltipContextData>(defaultContextData)
35+
const TooltipContext = createContext<TooltipContextDataWrapper>(DEFAULT_CONTEXT_DATA_WRAPPER)
3136

3237
const TooltipProvider: React.FC<PropsWithChildren> = ({ children }) => {
3338
const [anchorRefMap, setAnchorRefMap] = useState<Record<string, Set<AnchorRef>>>({

src/components/TooltipProvider/TooltipProviderTypes.d.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import type { ITooltipController } from 'components/TooltipController/TooltipCon
44
export type AnchorRef = RefObject<HTMLElement>
55

66
export interface TooltipContextData {
7-
getTooltipData: (tooltipId?: string) => {
8-
anchorRefs: Set<AnchorRef>
9-
activeAnchor: AnchorRef
10-
attach: (...refs: AnchorRef[]) => void
11-
detach: (...refs: AnchorRef[]) => void
12-
setActiveAnchor: (ref: AnchorRef) => void
13-
}
7+
anchorRefs: Set<AnchorRef>
8+
activeAnchor: AnchorRef
9+
attach: (...refs: AnchorRef[]) => void
10+
detach: (...refs: AnchorRef[]) => void
11+
setActiveAnchor: (ref: AnchorRef) => void
12+
}
13+
14+
export interface TooltipContextDataWrapper {
15+
getTooltipData: (tooltipId?: string) => TooltipContextData
1416
}
1517

1618
export interface ITooltipWrapper {

src/index-dev.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom'
33
import App from './App'
44
import './tokens.css'
55

6+
// eslint-disable-next-line no-console
67
console.log('Parent folder loaded react version: ', version)
78

89
ReactDOM.render(<App />, document.getElementById('app'))

0 commit comments

Comments
 (0)