core icon indicating copy to clipboard operation
core copied to clipboard

[Bug] 点击修改密码后后台白屏:ChangePasswordModal 在 Router 外调用 useNavigate

Open senshinya opened this issue 1 month ago • 3 comments

问题描述

使用官方镜像 innei/mx-server:15.6.1 的内置管理后台,点击“修改密码”会立即导致整个页面白屏。弹窗尚未出现,也没有输入或提交密码。刷新后页面恢复,再次点击可稳定复现。

复现步骤

  1. 使用所有者账号登录内置后台 /proxy/qaqdmin。
  2. 打开“设置 → 账号安全”(#/setting/account)。
  3. 点击“修改密码”。

预期:显示当前密码、新密码、确认密码三个输入框。

实际:整个 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,未找到相同报告。

senshinya avatar Oct 26 '26 16:10 senshinya

MXD-252

linear[bot] avatar Oct 26 '26 16:10 linear[bot]

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 call router.navigate('/login') in ChangePasswordModal instead of useNavigate(). 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

mainstreettools-hub avatar Oct 28 '26 13:10 mainstreettools-hub

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 (commit 8b8f0f5)
  • Compare: https://github.com/mx-space/core/compare/master...Henglie:fix/change-password-router?expand=1
  • Change: ChangePasswordModal no longer calls useNavigate(); PasswordSection (rendered inside the router) now passes an onLoggedOut callback through presentChangePassword.
  • Tests: the new regression test renders ModalRoot outside MemoryRouter, opens the modal, and checks the three password inputs — it passes on the branch and fails on master with useNavigate() may be used only in the context of a <Router> component.; typecheck and targeted eslint also pass.

No database schema changes.

修复已就绪,但我的账号无法通过 API 创建 PR(GraphQL 报 CreatePullRequest 权限错误,REST 返回 404),请维护者用上面的 compare 链接手动创建。

Henglie avatar Nov 13 '26 17:11 Henglie