Step by Step

React Router route changes: update the page title and move focus to the new heading

7 min read
Mikhail ShaymardanovWeb Quality and Accessibility Expert

The front-end developer who updates the page title on each React Router route has fixed one cue out of three. A full page load also put focus at the top and gave screen readers a new page to announce. React Router skips that load and leaves both jobs to the app. One focus call, on the new heading, does both: keyboard users start there, and screen readers read it.

From the link to the two-minute test


A screen reader says nothing after a React Router link because there is no page load to announce: the link swaps the view in place. The browser tab keeps its old title, focus never returns to the top, and screen readers get no page load to announce, a moment VoiceOver has a setting for. People who use the keyboard or a screen reader rely on the last two.

React Router's accessibility page lists focus management and announcements as things to consider, with no code, in a section for its framework mode. Our demo runs React Router 8.5.1 in declarative mode, the plain <BrowserRouter> setup. It is the member app of Wild Rye Farm Share, a vegetable box subscription, where a keyboard user opens “Deliveries” to skip next week's box.

Click the demo, then press the key shown, or move on with “Next step”. Escape leaves the demo.
3 steps

In the bare Wild Rye app, after Enter on “Deliveries”, the next Tab lands on “Add-ons”: the field “Find a delivery” only comes at the fourth press.

WebKit, Safari's engine, gave the same count with Option+Tab, since its plain Tab skips links. A link inside the old view fares better: “See past deliveries” vanishes with its view, focus falls back to the page, and the next press lands in the new view, on “Rate this box”.

A screen reader user who follows “Deliveries” may not learn that the page changed, as the 2019 user research cited in React Router's docs warns.


#How to change the page title and move focus on each React Router route?

Give each route its own <title>, then move focus to the new view's main heading.

#How to set the page title for each route?

Render a <title> in each route component: React 19 places it in the head from anywhere in the tree. React Router 8 needs React 19.2.7 or later and no longer ships react-router-dom. Import from react-router, which version 7 accepts too.

const SITE = "Wild Rye Farm Share";
 
function Deliveries() {
  return (
    <section>
      <title>{`Deliveries | ${SITE}`}</title>
      <h1>Your deliveries</h1>
      {/* the filter and the list of boxes */}
    </section>
  );
}

React wants a single string inside <title>. On our demo, <title>{name} | Wild Rye Farm Share</title> gave it two pieces, and the title came out empty, with no console warning. A template literal makes one string.

Keep one <title> at a time: none in the layout. WCAG's Page Titled guidance names single-page apps and asks for a page title that follows the view.

#Where focus should go after a route change?

WCAG leaves the choice to you: Focus Order only asks that the order let people understand and operate the page, and both patterns below do. Of the targets the 2019 user research tried, its screen reader testers liked the heading best: it would save time and make clear what happened.

After a retest, its final advice differs: a skip link that takes focus on each route change. A live region, an element whose new text screen readers read out, is present from page load and names the new page. On its magnification tester's phone, focusing a heading or wrapper much wider than the screen scrolled the view to its middle, cutting off both ends of the text. Samsung's Chrome seemed partly to blame. What suits one user with a disability, the research's author adds, may not suit another.

#Why we recommend focusing the heading?

We still recommend the h1, provided it fits a phone screen: on Wild Rye, “Your deliveries” takes 310 of 360 pixels, on one line. One move puts keyboard users at the start of the new content and gives screen readers its heading. MDN adds that live regions can behave differently across browser and screen reader pairs, and likely don't suit a change that moves focus.

Declarative mode has no ScrollRestoration to reset the scroll, so in a phone-sized window the bare app opened “Past deliveries” with its heading over 200 pixels above the screen. focus() fixes that: it scrolls the heading into view.

On the focused heading, the app's :focus-visible outline shows in Chrome after Enter only, in WebKit after a click too. Keep it: the sighted 2019 testers found visible focus useful, though one added that outlines on elements that do nothing can confuse. The voice control tester spotted the outline more easily on a heading than on a wide wrapper. Our :focus-visible guide covers the style.

Focus visible: how to style the focus indicator in CSS

Pick the 2019 research's pattern when views can't promise an h1 on render: a view behind a loading screen, a dashboard with none. Focus lands on a working skip link in the view, like the research's “skip to main navigation”, and nothing wide receives it. On Wild Rye, a top-of-page “Skip to main content” link costs one more key: Enter on it, then Tab, reaches “Find a delivery”.

It has two parts to maintain, and the live region must exist from the first render, in the app shell. Pick one pattern: a focused heading plus a region repeating its name would give screen readers that name twice.

How to add a skip link: the HTML code and the keyboard test

#How to write the focus hook?

The focus code is a small hook, called in the layout that renders <Outlet /> inside <main>. React runs effects after the new view is in the page.

Keyed on the location, as in React Router's own useLocation example, that hook broke Wild Rye's filter. The location includes the query string and hash, and the filter writes its text to the URL as ?q=: the first letter typed counts as a navigation. After “k”, focus jumped to the heading, and “ale” went nowhere, with the mouse as with the keyboard. Keyed on the pathname, the hook ignores the filter.

Avoid

Screenshot: the top of the Wild Rye “Your deliveries” view, with a rust outline around the heading and the letter k alone in the “Find a delivery” field.Marker 1. The heading “Your deliveries”, which took focus after the first letter. Marker 2. The “Find a delivery” field, holding only the letter k.
const location = useLocation();
const previous = useRef(location);
 
useEffect(() => {
  if (previous.current === location) return;
  previous.current = location;
  // focus the new h1
}, [location]);

Correct

Screenshot: the same view, with the rust outline around the “Find a delivery” field, which holds the word kale.Marker 2. The “Find a delivery” field, still focused, with the whole word kale typed.
const { pathname } = useLocation();
const previous = useRef(pathname);
 
useEffect(() => {
  if (previous.current === pathname) return;
  previous.current = pathname;
  // focus the new h1
}, [pathname]);
Keyed on the location, the hook sends focus to the headingmarker 1 as soon as “k” lands in “Find a delivery”marker 2. Keyed on the pathname, focus stays in the fieldmarker 2, and “kale” goes in.

The pathname comparison also leaves the first load to the browser. A “first run” boolean looks equivalent, but Strict Mode runs effects one extra time in development. On our demo, the boolean version sent focus to the heading on every load and reload in development, never in production.

The hook gives the heading tabindex="-1": a script can focus it, and Tab skips it. A view without an h1 falls back to <main>, which “worked okay” for the 2019 screen reader testers, though far less noticeable than a heading.

import { useEffect, useRef } from "react";
import { Outlet, useLocation } from "react-router";
 
export function useFocusOnRouteChange() {
  const { pathname } = useLocation();
  const previous = useRef(pathname);
 
  useEffect(() => {
    if (previous.current === pathname) return;
    previous.current = pathname;
    const target =
      document.querySelector("main h1") ?? document.querySelector("main");
    if (!target) return;
    if (!target.hasAttribute("tabindex")) target.setAttribute("tabindex", "-1");
    target.focus();
  }, [pathname]);
}
 
export function Layout() {
  useFocusOnRouteChange();
  return (
    <>
      <Header />
      <main>
        <Outlet />
      </main>
    </>
  );
}

#How to test a React Router route change with the keyboard and VoiceOver?

Each route takes about two minutes: four keys in the browser, then the same link with VoiceOver on. On a Mac, Safari's Tab skips links until “Press Tab to highlight each item on a webpage” is on in its Advanced settings, and buttons until Keyboard Navigation is on in macOS.

Click the demo, then press the key shown, or move on with “Next step”. Escape leaves the demo.
4 steps

In the fixed Wild Rye app, one Tab after the link lands in the new view, on “Find a delivery”, and typing “k” in it keeps focus in the field.

Then turn on VoiceOver with Command+F5 and follow the same link; Control pauses its speech. The new heading should be spoken once: silence or the name said twice means the hook needs another look. Our runs had no screen reader, so the listening part is for a real Mac.

VoiceOver is the reader on a developer's Mac, but only 6.5% of respondents to WebAIM's 2026 screen reader survey use it as their main desktop reader. Nine in ten use Windows, so repeat the check with NVDA.

With the title and focus back, a keyboard user skips next week's box in four keys from “Deliveries”: Enter, Tab twice, then Enter on Thursday, November 19, whose button now reads “Box skipped”. The bare app took seven, through “Add-ons”, “Recipes” and “Account” first.

NVDA screen reader: how to install it, set it up and test a web page

Includdy analyzes the page on screen and can add it to a snapshot, a dated analysis of the site's pages. Its manual checks there ask whether the page title describes the page and whether each visible heading, the focused h1 included, is marked as one.


Share this article

Frequently asked questions

Yes. Next.js includes a route announcer by default. It looks for the page name in document.title, then in the <h1>, then in the URL path, as its accessibility docs explain, and the same docs ask for a unique, descriptive title on every page.

React Router has no such announcer: its accessibility page leaves focus management and announcements to the app.

Use a status message: an element with role="status", present from the first render, whose text changes, such as "2 boxes found". MDN says a live region must exist before its content changes, and a status region is polite: the screen reader speaks the change when the user is idle.

WCAG's Status Messages criterion covers a line like "18 results returned". A route change is a change of context, which that criterion leaves out of its scope.

No rule names it. The Justice Department's web rule makes WCAG 2.1 Level AA the technical standard for the web content and mobile apps of state and local governments. An interim final rule of April 2026 moved its compliance dates. Governments with a total population of 50,000 or more have until April 26, 2027; smaller ones and special district governments, until April 26, 2028.

Within those guidelines, Page Titled asks for a title that describes each view, and Focus Order asks for a logical order without saying where focus goes. For a business open to the public, the department has no regulation setting out detailed web standards, though it reads the ADA's general rules as covering websites.