如何在React中从组件数组中传递prop给组件

  • 本文关键字:组件 prop 数组 React reactjs jsx
  • 更新时间 :
  • 英文 :


假设你有一个组件数组:

var my_array = [ <SomeComponent1 textcolor={defaultFontColor} />, <MyComponent2 textcolor={defaultFontColor} />}, <SomeComponent3 textcolor={defaultFontColor} />}, ...]

您想遍历数组,但也要向组件传递props(在本例中为AComponent):

my_array.map(AComponent => {
return (
<View>
{AComponent}
</View>
)
})

在这个例子中,我如何将道具传递给{AComponent}?

将组件视为json对象

AComponent = {
props:{
prop1: "value for the prop1"
}
}

现在你可以传递你想要的道具了,比如

my_array.map(AComponent => {
AComponent.props["newProp"] = "propValue"
return (
<View>
{AComponent}
</View>
)
})

或者,如果你想使用数组中push组件时传递的props,你可以直接在

组件的render()方法中使用它们

最新更新