说我有一个像这样的父组件:
class ParentComponent{
constructor(props){
this.state = {
children:[{...},{...}...]
}
render(){
return(
<ChildComponentList .../>
)
}
}
}
还有这样的子列表:
class ChildComponentList{
constructor(props){
this.state = {
...
}
render(){
return(
???.children.map(
<ChildComponent ... />
)
)
}
}
}
现在,我显然希望将孩子从父级传递到孩子列表。我知道您使用道具将数据从父母传递给孩子。但是说<ChildComponentList children = {this.children} />
之类的东西对我来说没有任何意义,因为孩子不是一个单一的价值而是一个庞大的清单。还要假设这将是一棵树,其中ChildComponent
可以包含更多ChildComponentList
,依此类推。
以下是我可以看到的选项:
getChildren()
方法,子级列表中会调用它最好的方法是什么?
答案 0 :(得分:1)
class ParentComponent{
constructor(props){
super(props);
this.state = {
items:[{...},{...}...]
}
}
render(){
return(
<ChildComponentList items={this.state.items} />
)
}
}
class ChildComponentList{
render(){
return(
this.props.items.map(
(item, i) => <ChildComponent key={i} ... />)
)
}
}