gpt4 book ai didi

reactjs 传递组件数组

转载 作者:行者123 更新时间:2023-12-05 02:05:14 25 4
gpt4 key购买 nike

我正在尝试将一组组件传递给另一个组件并在添加其他 Prop 后渲染它们

import {ComponentB} from ".../ComponentB";


<ComponentA
list={[
<ComponentB title={'title 1'} />,
<ComponentB title={'title 2'} />,
<ComponentB title={'title 3'} />
]} />

如何循环 ComponentA 中的 list 属性?这行不通:

//组件A

    { list.map((Item, index) => {
return (
<Item otherProps={'value xxx'}/>
)
})}

这个可以,但是不能加props

    { list.map((Item, index) => {
return (
{Item}
)
})}

最佳答案

只传递组件,不要在父组件中调用组件:这样,ComponentA 可以使用所有需要的属性调用组件:

<ComponentA 
list={[
{ Comp: ComponentB, title: 'title 1' },
{ Comp: ComponentB, title: 'title 2' },
{ Comp: ComponentB, title: 'title 3' },
]}
/>
// ComponentA
{ list.map(
({ Comp, title }, index) => <Comp title={title} otherProps={'value'} />
)}

关于reactjs 传递组件数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63708605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com