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:)