Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何将大量数据从父组件传递到子组件_Reactjs_Redux - Fatal编程技术网

Reactjs 如何将大量数据从父组件传递到子组件

Reactjs 如何将大量数据从父组件传递到子组件,reactjs,redux,Reactjs,Redux,假设我有这样一个父组件: class ParentComponent{ constructor(props){ this.state = { children:[{...},{...}...] } render(){ return( <ChildComponentList .../> ) } } } class ChildComp

假设我有这样一个父组件:

class ParentComponent{
    constructor(props){
        this.state = {
            children:[{...},{...}...]
        }
     render(){
         return(
             <ChildComponentList .../>
         )
     }
    }
}
class ChildComponentList{
    constructor(props){
        this.state = {
            ...
        }
     render(){
         return(
             ???.children.map(
                 <ChildComponent ... />
             )
         )
     }
    }
}
类父组件{
建造师(道具){
此.state={
子项:[{…},{…}]
}
render(){
返回(
)
}
}
}
还有这样的子列表:

class ParentComponent{
    constructor(props){
        this.state = {
            children:[{...},{...}...]
        }
     render(){
         return(
             <ChildComponentList .../>
         )
     }
    }
}
class ChildComponentList{
    constructor(props){
        this.state = {
            ...
        }
     render(){
         return(
             ???.children.map(
                 <ChildComponent ... />
             )
         )
     }
    }
}
类子组件列表{
建造师(道具){
此.state={
...
}
render(){
返回(
?儿童地图(
)
)
}
}
}
现在,我显然想将子对象从父对象传递到子对象列表。我知道您使用道具将数据从家长传递给孩子。但对我来说,说像
这样的话是没有意义的,因为孩子不是一个单一的价值观,而是一个巨大的列表。还假设这是一个树,
ChildComponent
可以包含更多的
ChildComponentList
s等等

以下是我可以看到的选项:

  • 在父对象中使用某种
    getChildren()
    方法,并让子对象列表调用它
  • 使用Redux并将子项放入数据存储中
  • 让每个子组件列表分别从数据库检索其数据,这样我就不必在树下传递一个大列表
  • 尽管对我来说没有意义,但还是继续把整个清单放在道具里吧
执行此操作的最佳方法是什么?

类ParentComponent{
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} ... />)
         )
     }
}
建造师(道具){ 超级(道具); 此.state={ 项目:[{…},{…}] } } render(){ 返回( ) } } 类子组件列表{ render(){ 返回( 此.props.items.map( (项目一)=>) ) } }
为什么将其作为道具传递对您没有意义?我支持@SakoBu。如果你已经在使用redux,那么就使用redux,否则仅仅为了这个而引入redux就有点过头了。我曾认为道具是类似于html属性的东西,因为这就是ir的外观,但我现在看到的远不止这些。是的,@jimboweb。当您查看代码时,这“听起来像HTML”,但不要忘记,这是JSX,是一个“XML表示”操作系统Javascript代码,而不是HTML:)