[Bug] 点击修改密码后后台白屏:ChangePasswordModal 在 Router 外调用 useNavigate
问题描述
使用官方镜像 innei/mx-server:15.6.1 的内置管理后台,点击“修改密码”会立即导致整个页面白屏。弹窗尚未出现,也没有输入或提交密码。刷新后页面恢复,再次点击可稳定复现。
复现步骤
- 使用所有者账号登录内置后台
/proxy/qaqdmin。 - 打开“设置 → 账号安全”(
#/setting/account)。 - 点击“修改密码”。
预期:显示当前密码、新密码、确认密码三个输入框。
实际:整个 React 页面白屏,控制台报错:
Uncaught Error: useNavigate() may be used only in the context of a <Router> component.
at ... (cn-hWc6MTtu.js:1:1092)
at ... (cn-hWc6MTtu.js:1:63810)
at ... (cn-hWc6MTtu.js:1:63791)
at Cn (page-De_1iKRX.js:1:23433)
源码定位
- ChangePasswordModal.tsx(v15.6.1) 在组件渲染时调用
useNavigate(),用于成功改密、退出登录后导航到/login。 - App.tsx 将
RouterProvider放在AppProviders的 children 中。 - providers.tsx 将
ModalRoot放在 children 之后。因此ModalRoot与RouterProvider是兄弟节点,命令式弹窗拿不到 Router 上下文。
这与浏览器中的异常堆栈一致。检查时,上游 master 的 ChangePasswordModal 也仍有相同的 useNavigate() 调用。
可考虑由 Router 内的调用方将导航回调作为 prop 传给弹窗,或使用共享 router 实例的导航方法。建议增加使用真实 App provider 层级的回归测试:点击按钮后能显示三个密码输入框,并用 mock API 覆盖成功改密后的退出登录与导航。
环境
- Core / bundled Admin:v15.6.1,官方 Docker 镜像
- 宿主:Debian 13 / arm64
- 镜像内 Node.js:26.12.1
- 浏览器:Chromium 155,macOS
- 通过 HTTPS 访问内置后台
已搜索 useNavigate、ChangePasswordModal、修改密码等相关 issue/PR,未找到相同报告。
Since ModalRoot renders as a sibling of RouterProvider, any hook-based navigation inside an imperatively opened modal will throw. Two fixes that avoid restructuring the providers:
- Export the router you pass to RouterProvider (the
createHashRouter(...)instance) and callrouter.navigate('/login')in ChangePasswordModal instead ofuseNavigate(). That works outside the React tree. - Or have the caller, which is inside the router, pass
onLoggedOut={() => navigate('/login')}into the modal props.
Either way, a regression test that renders the real AppProviders tree and clicks "修改密码" (Change password) will catch this, because the error throws on render before any input. I'd also grep the other modals for useNavigate or useLocation, since anything opened via ModalRoot has the same problem.
Write-up of this error: https://getfixstack.com/errors/react-router-usenavigate-outside-router/?ref=gh
The fix is ready on a branch, but I could not open a PR from my account: GraphQL CreatePullRequest returns does not have the correct permissions, and REST POST /repos/mx-space/core/pulls returns 404. Posting it here so a maintainer can open it via the compare link.
- Branch:
Henglie:fix/change-password-router(commit8b8f0f5) - Compare: https://github.com/mx-space/core/compare/master...Henglie:fix/change-password-router?expand=1
- Change:
ChangePasswordModalno longer callsuseNavigate();PasswordSection(rendered inside the router) now passes anonLoggedOutcallback throughpresentChangePassword. - Tests: the new regression test renders
ModalRootoutsideMemoryRouter, opens the modal, and checks the three password inputs — it passes on the branch and fails on master withuseNavigate() may be used only in the context of a <Router> component.;typecheckand targetedeslintalso pass.
No database schema changes.
修复已就绪,但我的账号无法通过 API 创建 PR(GraphQL 报 CreatePullRequest 权限错误,REST 返回 404),请维护者用上面的 compare 链接手动创建。