【发布时间】:2020-09-05 06:46:23
【问题描述】:
我想使用.map() 遍历自定义 React 元素。我正在循环的一个例子是:
<Button text="Click me" />
<Button text="Click me" bgColor="yellow" textColor="black"/>
<Button text="Click me" bgColor="limeGreen" onClick={() => console.log('clicked')}/>
<Button text="Click me" bgColor="orchid"/>
<Button text="Click me" bgColor="rgb(150, 20,0)"/>
我有一个名为 Container 的组件,它将这些子元素作为属性:
export const Container = ({children}) => {
return (
<div>{children}</div>
)
}
我尝试像这样实现循环:
const newChildren = children.map((item) => {
//add class name to every item
})
//
<div>{newChildren}</div>
但是我被困在这一点上。如何将className attr 添加到所有项目?
【问题讨论】:
标签: javascript reactjs array.prototype.map