Skip to content

Commit 09d1352

Browse files
committed
Best way to deal with tooltip component in React
1 parent da0aae4 commit 09d1352

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

packages/lab-example/src/index.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ const plugin: JupyterFrontEndPlugin<void> = {
168168
};
169169

170170
function Artwork(props: { dataRef: React.Ref<WebDataGrid> }): JSX.Element {
171-
const tooltipAnchor = React.useRef<HTMLElement>(null);
171+
const [tooltipAnchor, setTooltipAnchor] = React.useState<HTMLElement | null>(
172+
null
173+
);
172174

173175
const onEvent = (event: any) => {
174176
console.log(event);
@@ -275,9 +277,15 @@ function Artwork(props: { dataRef: React.Ref<WebDataGrid> }): JSX.Element {
275277
</div>
276278
<div className="jp-FlexColumn">
277279
<label>Tooltip</label>
278-
{/* The tooltip component needs to be placed after the anchor so the reference is defined */}
279-
<Button ref={tooltipAnchor}>Anchor</Button>
280-
<Tooltip anchorElement={tooltipAnchor.current}>React tooltip</Tooltip>
280+
{/* Use callback ref to react to the component mount */}
281+
<Button
282+
ref={(anchor: HTMLElement) => {
283+
setTooltipAnchor(anchor);
284+
}}
285+
>
286+
Anchor
287+
</Button>
288+
<Tooltip anchorElement={tooltipAnchor}>React tooltip</Tooltip>
281289
</div>
282290
</div>
283291
<div className="jp-FlexColumn" style={{ gridColumn: 4 }}>

0 commit comments

Comments
 (0)