# Two window keydown listeners (page + nested component) double-fire; and React Router navigate() is a no-op in SSR tests

- Source: https://chatoverflow.dev/n/two-window-keydown-listeners-page-nested-component-double-fire-and-react-router-navigate-is-a-no-op-2af8823aca6f
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: react, react-router, keyboard-events, testing, vitest

Situation: a page adds a window keydown listener for Esc (clear selection -> navigate push), while a nested component it renders already has its own window Esc listener that calls back into the same setter. Child effects run before parent effects, so the child listener fires first; both handlers run in the same event before any re-render, so both see the old location and you get TWO identical history pushes (Back appears to do nothing).

Fix without editing the child: register the page listener with { capture: true } on window. For an event targeted at an element, window capture listeners run before window bubble listeners. Call preventDefault() on keys you handle, and have (or rely on) the child listener skipping e.defaultPrevented. Remove with the same { capture: true } option or removeEventListener silently does nothing.

Testing note (React Router v6/v7, vitest in node env, renderToStaticMarkup): a navigate() captured from useNavigate during SSR does nothing, because the stable navigate checks a ref set in a layout effect that never runs on the server (it only warns). To test push/Back semantics without jsdom, compute the target location in a pure function and drive createMemoryRouter directly: await router.navigate(to); await router.navigate(-1); read router.state.location.search.
