Repository navigation
Description
Description
useHoverCard() returns a new callback ref and interactionRef on every rerender, even when nothing the listeners capture has changed. React then calls the old ref with null and the new one with the element. HoverCard removes its trigger listeners and adds them back, and it removes and re-applies the anchor-name inline style on the trigger.
This is the same root cause as #5949, which #5951 fixed for Tooltip only. useTouchTrigger still returns a fresh object literal each render. useHoverCard lists the whole touch object as a dependency of handleMouseEnter, handleMouseLeave, handlePointerDown, handleFocusIn and renderHoverCard, and so of the interactionRef and ref built from them.
Expected: HoverCard trigger refs stay stable across rerenders that do not change their behavior, as Tooltip's now do.
Impact: a chat app renders one HoverCard per prompt in a navigation rail, and the rail re-renders while a reply streams. With 30 triggers the anchor-name rewrites were about 1,200 inline style writes per second, the top style-invalidation source during streaming. They also caused 50–80 ms forced style recalcs.
Reproduction
Verified on main (d72e4d6) with the repo's vitest setup. Both tests below fail.
import {describe, expect, it} from 'vitest';
import {render, renderHook} from '@testing-library/react';
import {useHoverCard} from './useHoverCard';
describe('useHoverCard identity', () => {
it('keeps callback refs stable across unchanged rerenders', () => {
const {result, rerender} = renderHook(() => useHoverCard());
const firstRef = result.current.ref;
const firstInteractionRef = result.current.interactionRef;
rerender();
expect(result.current.ref).toBe(firstRef); // fails
expect(result.current.interactionRef).toBe(firstInteractionRef); // fails
});
it('does not rebind trigger listeners on rerender', () => {
function Harness() {
const card = useHoverCard();
return (
<div>
<span ref={card.ref}>Trigger</span>
{card.renderHoverCard(<div>Card</div>)}
</div>
);
}
const {getByText, rerender} = render(<Harness />);
const trigger = getByText('Trigger');
const added: string[] = [];
const add = trigger.addEventListener.bind(trigger);
trigger.addEventListener = ((type: string, ...rest: unknown[]) => {
added.push(type);
return (add as (...args: unknown[]) => void)(type, ...rest);
}) as typeof trigger.addEventListener;
rerender(<Harness />);
expect(added).toEqual([]); // fails: pointerenter, mouseenter, mouseleave, pointerdown
});
});On one plain rerender the trigger gets removeEventListener for 6 events and addEventListener for 4 (plus focusin/focusout when the trigger is focusable). Its style attribute is mutated twice.
Memoizing the return value in useTouchTrigger fixes both tests. All of its members are already stable: isTouchPointerRef is a ref and the rest are useCallbacks. The existing HoverCard, Tooltip and Layer tests still pass (242 tests).
return useMemo(
() => ({
isTouchPointerRef,
isTouchInteraction,
handlePointerEnter,
handlePointerDown,
clearTapOpen: disarmOutsideDismiss,
}),
[isTouchInteraction, handlePointerEnter, handlePointerDown, disarmOutsideDismiss],
);Destructuring in useHoverCard, as #5951 did for Tooltip, would also work. Memoizing at the source covers every consumer of useTouchTrigger. Happy to send a PR.
Astryx Version
0.6.2 (latest), and main at d72e4d6
Environment
Electron 43 (Chromium) on macOS in our app; the reproduction above runs in the repo's jsdom vitest setup.
Activity
perf(HoverCard): keep trigger refs stable across rerenders (meta#6606)