0Pricing
Web Accessibility Academy · 课时

无需重新加载页面的路由变更

在路由变更时播报导航并移动焦点。

无需重新加载页面的路由变更 是 CoddyKit 上的免费 Web Accessibility Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web Accessibility Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web Accessibility Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Silent Navigation Problem

In a SPA, clicking a link swaps content without a full reload. To a screen reader, nothing seems to happen, so it stays silent about the new page. 🤫

Why Full Reloads Were Easy

A real page load resets focus and makes the screen reader read the new title. Client-side routing skips all of that, so you must recreate those cues yourself.

Update the Document Title

On every route change, set document.title to match the new view. Many screen readers read the title when it changes, giving users their bearings.

useEffect(() => {
  document.title = 'Settings - MyApp';
}, [location.pathname]);

Move Focus on Navigation

After routing, send focus to the top of the new content, usually the main heading. This tells keyboard and screen reader users the page has changed.

Focus the Heading or Main

Give the target a tabindex of -1 so it can receive focus by script without joining the tab order. Then call .focus() on it after the route updates.

<h1 tabindex="-1" ref={headingRef}>Settings</h1>

Announce With a Live Region

An alternative is a visually hidden live region that you fill with text like Navigated to Settings. The screen reader speaks it without moving focus.

<div aria-live="polite" className="sr-only">{message}</div>

Pick One Strategy, Not Both

Moving focus and announcing in a live region both work. Doing both at once can cause double announcements, so choose one approach per app and stay consistent.

Timing Matters

Run your focus or announcement after the new view renders. In React, an effect that depends on the route path fires once the DOM is updated.

Do Not Yank Focus Mid-Task

Only move focus on a genuine route change, not on minor in-place updates. Stealing focus while someone is mid-action is jarring and confusing.

Router Libraries Can Help

Some routers expose hooks for route changes. React Router gives you useLocation, so an effect can react every time the path actually changes.

const location = useLocation();
useEffect(() => focusMain(), [location.pathname]);

Test It Like a User

Turn on a screen reader, click through your nav, and listen. If route changes are announced and focus lands sensibly, your SPA navigation is accessible.

Quick Check

A screen reader user clicks a SPA link but hears nothing change. What is the most likely cause?

Recap: Announce the Route

SPA navigation is invisible until you make it heard. Update the title, then move focus or use a live region so everyone knows the page changed. ✅

常见问题解答

「无需重新加载页面的路由变更」课时是免费的吗?

是的 — 「无需重新加载页面的路由变更」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web Accessibility Academy 课程的其余内容,请升级到 CoddyKit PRO。 Web Accessibility Academy 课程共包含 4 节课。

「无需重新加载页面的路由变更」这节课中我会学到什么?

在路由变更时播报导航并移动焦点。 你通过在浏览器中直接运行的动手代码来练习 Web Accessibility Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web Accessibility Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web Accessibility Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「无需重新加载页面的路由变更」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web Accessibility Academy 课中编写并运行代码吗?

能。每节 Web Accessibility Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 无需重新加载页面的路由变更
  2. 使用 Refs 和 useEffect 管理焦点
  3. Fragments、Portals 与 DOM 顺序陷阱
  4. jsx-a11y 与组件级防护
← 返回 Web Accessibility Academy