[英]How do I pass children index number to a react component?
My Data looks like the below. 我的数据如下所示。
const data = { main: { 88: { issues: [ { id: 1 }, { id: 3 }, { id: 4 } ] } } };
I am looping through data.main
and then passing some data to the child component like this. 我循环遍历
data.main
,然后将一些数据传递给子组件,就像这样。
{Object.values(data.main).map((key) => { <Issues id={key} issueIndex={XXX} {...this.props} /> }
But I also want to pass an index of all issues to the child element. 但我也想将所有问题的索引传递给子元素。 So that I can number it from here.
所以我可以从这里编号。
My attemp inside jsx file below. 我在下面的jsx文件中尝试。
{Object.values(data.main).map((group, key) => { let issuesArr = group.issues; {issuesArr.map((value, index) => { index + 1; })} <Issue id={key} issueIndex={XXX} {...this.props} /> }
I want to pass a number as issueIndex
to the <Issue />
I have no control of the data structure. 我想将一个数字作为
issueIndex
传递给<Issue />
我无法控制数据结构。
That is happening because your issuesArr.map
isn't doing anything. 这是因为您的
issuesArr.map
没有做任何事情。 Change your code to this: 将您的代码更改为:
{
Object.values(data.main).map((group, key) => {
let issuesArr = group.issues;
{
issuesArr.map((value, index) => {
return (
<Issue
id={key}
issueIndex={value.id}
{...this.props}
/>
)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.