Bug: React Router shows 空白 page for unknown route
错误信息
否 route matched current URL
快速摘要
用户导航到 URL 那个 doesn't match 任何定义 route, and 否 catch-所有 route exists.
为什么发生
否 catch-所有 route (
path="*") 定义Route path doesn't match URL 模式
Typo in route path
最小示例
✗ 错误代码
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
{/* No catch-all! */}
</Routes>✓ 修复代码
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<NotFound />} />
</Routes> path="*" route matches 任何 URL 那个 doesn't match 定义 routes, showing 404 page 相反的 空白 screen.
如何诊断
检查所有定义 routes and 它们的 paths
验证 URL 用户导航到
Look for catch-所有 route
如何修复
添加 catch-所有 route:
<Route path="*" element={<NotFound />} />Fix route path 到 match URL
添加 redirect for 常见 misspellings
如何预防
总是包含 catch-所有 404 route
Use route constants 到 avoid typos
相关资源
相关术语
相关课程
- React Router
学习 routing and catch-所有 routes.