React 的props传递是否只能传递对象,而不能传递数组?
React 的props传递是否只能传递对象,而不能传递数组?我有如下的数组数据:
const treeData: DataNode[] = [
{
title: 'parent 0',
key: '0-0',
children: [
{ title: 'leaf 0-0', key: '0-0-0', isLeaf: true, selectable: false },
{ title: 'leaf 0-1', key: '0-0-1', isLeaf: true, selectable: false },
],
selectable: false,
icon: false,
switcherIcon: 'o',
},
{
title: 'parent 1',
key: '0-1',
children: [
{ title: 'leaf 1-0', key: '0-1-0', isLeaf: true, selectable: false },
{ title: 'leaf 1-1', key: '0-1-1', isLeaf: true, selectable: false },
],
selectable: false,
icon: false
},
];
传递数据到:
...
<TreeDirectory {...treeData}></TreeDirectory>
我在组件内接受的时候,会变成对象:
所以,是否React 的props传递是否只能传递对象,而不能传递数组?如果想要传递数组应该怎么传递呢?对treeData 进一步封装是吗?
回复
1个回答

test
2024-06-27
<TreeDirectory data={treeData}></TreeDirectory>
类似这样传数组
就是说,用一个变量传数组
如果类似 <TreeDirectory {...treeData}></TreeDirectory>
这样传参,等价于 <TreeDirectory 0={treeData[0]} 1={treeData[0]}></TreeDirectory>
,当然接收到的是个对象
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容