【发布时间】:2020-12-10 13:16:07
【问题描述】:
我想将 props noOfIcons 传递给我的 ChartLegend 组件,如果没有 noOfIcons 值,那么我想渲染给定数量的图标。因此,如果 noOfIcons = 2 则渲染 2 <Icon/>。我尝试了以下方法,但它始终只呈现 1 <Icon/>,我错过了什么? &如果有一个简单的方法来处理这个请建议。非常感谢。
<ChartLegend iconName={"square-full"} noOfIcons={2} label={'label'} />
const ChartLegend = props => {
const { iconName, label, noOfIcons = 2 } = props;
const renderIcons = () => {
if (!noOfIcons) {
return <Icon
name={iconName}
type='solid'
/>;
}
else {
for (let i = 0; i < noOfIcons; i++) {
return <Icon
name={iconName}
type='solid'
/>;
}
}
};
return (
<Col>
{renderIcons()}
{label}
</Col>
);
};
export default ChartLegend;
【问题讨论】:
标签: javascript reactjs loops