[英]React.js How to loop and render component inside component?
I'm trying to render components according to the data list.我正在尝试根据数据列表呈现组件。 The data list include a list of address and records, in each address records, it contains one or multiple member records.数据列表包括地址和记录列表,在每个地址记录中,它包含一个或多个成员记录。 The data structure looks like this:数据结构如下所示:
combineData:[
addressData:{addressNo:... },
memberDataList:[{...}]
]
I want to render like this:我想这样渲染:
<address1>
<member1 of address 1>
<member2 of address 1>
<address2>
<member1 of address 2>
<member2 of address 2>
<member3 of address 2>
......
class AddressMembersTable extends React.Component {
addressMembersViews = () => this.props.combineData.map((combineData, index) => {
return (
<Container layout="vbox">
<AddressField
ref={'addressField'}
key={'addressField'}
addressData={combineData.addressData}
/>
{combineData.memberDataList.forEach((memberData,index)=>{
return(
<MemberField
ref={'memberField_' + index}
memberData = {combineData.memberDataList}
/>
)
})}
</Container>
)
})}
I tried to loop the memberDataList for the MemberField components, but only AddressField is able to show.我试图为 MemberField 组件循环 memberDataList,但只有 AddressField 能够显示。 How can i render the components correctly?如何正确渲染组件?
You will map it the say way you did this.props.combineData
.你会 map 说你做this.props.combineData
的方式。 Array.prototype.forEach
is a void return . Array.prototype.forEach
是一个 void return 。 You need to return the array of JSX you mapped to.您需要返回映射到的 JSX 数组。
{combineData.memberDataList.map((memberData, index) => (
<MemberField
key={index}
ref={'memberField_' + index}
memberData = {combineData.memberDataList}
/>
))}
You are using the wrong list function.您使用了错误的列表 function。
map
- transforms a list map
- 转换列表
forEach
- performs side effects with each list item forEach
- 对每个列表项执行副作用
const list = [1, 2, 3]
const double = n => n * 2
const a = list.map(double)
console.log(a) // [2, 4, 6]
const b = list.forEach(double)
console.log(b) // undefined
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.