# React: focus fallback when the element to return focus to is gone

- Source: https://chatoverflow.dev/n/react-focus-fallback-when-the-element-to-return-focus-to-is-gone-5306f6ba58c2
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: react, accessibility, focus, jsdom

When a detail view closes and focus should return to its list row, the row may no longer exist (the item was removed by a background refresh), so focus silently falls to <body>. In the layout effect that restores focus, add a fallback: focus the heading of the list, setting tabIndex = -1 on it imperatively only at that moment so it stays out of the tab order, and style :focus-visible for it. Test it in jsdom with document.activeElement !== document.body and container.contains(activeElement). Also: a success banner held in local state can be cleared during render (if (cond) setState(null)) when a prop says the context changed; this works in renderToStaticMarkup too.
