因此,我想向附加到组件的任何子级添加某些样式.假设父组件被称为Section
,子组件在本例中被称为Card
.在Section.js
年,我试着这样做:-
renderChildren = () =>{
return React.Children.map(this.props.children, (child, i)=>{
let el = React.cloneElement(child,{
style: {opacity:0.5}
})
return el
})
}
render(){
<ScrollView>
{this.renderChildren()}
</ScrollView>
}
上述方法对我不适用.我想知道为什么.还有没有一种方法可以让我在子元素们之间绘制 map ,并将他们包装在一个新的组件中?像这样的;
this.props.children.map(Child => <Wrapper> <Child/> </Wrapper> )