← Back to React, page 8

react-0149 · Hard senior

Why does a single-page app need focus management on route changes, and how do you implement it accessibly?

On a normal page load, a browser resets focus and a screen reader announces the new page. In a single-page app, the URL and content change but focus stays on the clicked link, which may even be removed, so keyboard and screen reader users get no signal. After navigation, move focus to the new page heading (with tabIndex of -1), or to the main container, update document.title, and consider a polite live region announcement. Do not steal focus on the initial load.

Full answer

Full page loads give assistive technology several signals: the document title is read, focus returns to the top, and the reading position resets. A client-side router replaces content without those signals. Typical failures: focus stays on a link that no longer exists, so it jumps to the body, a screen reader says nothing, and a keyboard user must tab from the top through the header again.

Implementation options:

  1. Move focus to the main heading. On route change, focus the <h1> of the new page. Headings are not focusable by default, so give them tabIndex={-1}, which allows focus() without adding them to the tab order. The screen reader then reads the heading.
  2. Focus a container such as <main tabIndex={-1}> when headings differ in structure.
  3. Announce through a live region: a visually hidden element with aria-live="polite" whose text is updated with the page title.
  4. Update document.title (React 19 supports rendering <title> in components) so the title is accurate for each route.
  5. Restore scroll position appropriately (routers provide this) and keep skip links working.

Details:

  • Do not move focus on the initial page load, only on later navigations, so it does not interrupt the browser's normal behaviour.
  • Use an effect keyed by the location (useEffect with the pathname) or the router's navigation hooks, and focus after the new content has rendered.
  • Frameworks help: Next.js and Remix provide route announcers and some handling, React Router offers primitives such as ScrollRestoration, but you still need to check the result with a screen reader.
  • For modals and menus, move focus in and return it to the trigger on close.
  • Test with keyboard only and with a screen reader (NVDA, VoiceOver), plus automated checks with axe, which cannot find this issue by itself.

The example uses a tiny state-based router and focuses the heading after each navigation except the first.

jsx
import { useEffect, useRef, useState } from 'react';

export default function App() {
  const [page, setPage] = useState('Home');
  const headingRef = useRef(null);
  const firstRender = useRef(true);

  useEffect(() => {
    document.title = page + ' | Schoolabe';
    if (firstRender.current) {
      firstRender.current = false;
      return;
    }
    headingRef.current.focus();
  }, [page]);

  return (
    <div>
      <nav>
        {['Home', 'Courses', 'About'].map((name) => (
          <a key={name} href={'#' + name} onClick={(event) => { event.preventDefault(); setPage(name); }}>
            {name}
          </a>
        ))}
      </nav>
      <main>
        <h1 ref={headingRef} tabIndex={-1}>{page}</h1>
      </main>
    </div>
  );
}

The check clicked Courses and About. After each navigation the heading received focus. On the first load, focus stayed on the document body. Output of the run: first load: heading "Home", focus on "HomeCoursesAboutHome" after clicking Courses: heading "Courses", focus on "Courses" after clicking About: heading "About", focus on "About"

Key points

  • SPA navigation does not reset focus or announce the new page, which hurts keyboard and screen reader users.
  • Focus the new h1 (tabIndex -1) or main container after the route changes, and update document.title.
  • Use a polite live region as an alternative or addition for announcements.
  • Skip focus on the initial load, and focus after the new content has rendered.
  • Verify manually with keyboard and a screen reader, because automated checks miss it.

Common mistakes

  • Leaving focus on a link that disappeared after navigation.
  • Calling focus() on a heading without tabIndex -1.
  • Moving focus on the very first page load.
  • Relying only on automated accessibility tools.
  • Updating content without changing document.title.

Follow-ups

  • How would a live region announcement differ from moving focus?
  • How do modals return focus to their trigger?
  • What do Next.js and React Router do about this by default?

Tips

  • Describe what browsers do on a full load and what a SPA lacks.
  • Give the heading focus recipe with tabIndex -1.
  • Mention manual testing with a screen reader.