react
type_error
ai_generated
true
类型错误:X 不是一个函数。(在 'X()' 中,'X' 未定义)当对 children 使用 .map 时
TypeError: X is not a function. (In 'X()', 'X' is undefined) when using .map on children
ID: react/invalid-array-children-map
93%修复率
88%置信度
1证据数
2023-06-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| React 18.2.0 | active | — | — | — |
| React 17.0.2 | active | — | — | — |
根因分析
children 属性不是一个数组,而是一个单独的 React 元素、字符串或 undefined,因此对其调用 .map() 会失败。
English
The 'children' prop is not an array but a single React element, string, or undefined, so calling .map() on it fails.
官方文档
https://react.dev/reference/react/Children#children-map解决方案
-
Use React.Children.toArray(children) before calling .map() to safely convert children to an array, regardless of type.
-
Add a guard to check if children is an array before mapping. If not, wrap it in an array or handle single child separately.
无效尝试
常见但无效的做法:
-
70% 失败
If children is already an array (multiple children), this creates a nested array; .map won't iterate over individual children correctly.
-
60% 失败
React.Children.toArray returns an empty array for null/undefined, so .map works, but the real issue is that children might be a single element not in an array, which toArray handles but the user may not realize why.