react
type_error
ai_generated
true
类型错误:无法读取 undefined 的属性(读取 'params')
TypeError: Cannot read properties of undefined (reading 'params')
ID: react/typeerror-cannot-read-properties-of-undefined-reading-params
90%修复率
86%置信度
1证据数
2023-12-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| React Router 6.20.0 | active | — | — | — |
| React Router 5.3.4 | active | — | — | — |
| React 18.2.0 | active | — | — | — |
根因分析
代码尝试访问路由参数(例如 `useParams()` 或 `match.params`),但组件未在 Route 上下文内渲染,或路由路径未定义预期的参数。
English
Code attempts to access route parameters (e.g., `useParams()` or `match.params`) but the component is not rendered within a Route context, or the route path does not define the expected parameter.
官方文档
https://reactrouter.com/en/main/hooks/use-params解决方案
-
Ensure the component is wrapped in a Route: `<Route path="/user/:id" element={<UserComponent />} />` and access params via `useParams()` hook inside UserComponent. -
If using class component, use `withRouter` HOC (React Router v5) or wrap with `useParams` in a function component and pass as props.
-
Check that the route path includes the parameter (e.g., `/user/:id`) and the URL actually matches.
无效尝试
常见但无效的做法:
-
90% 失败
The underlying issue (component outside Router) remains, leading to other bugs.
-
85% 失败
Both exports exist, but the context provider difference is the real issue.