【发布时间】:2020-10-09 21:48:14
【问题描述】:
我有四个组件 Rectangle、Circle、Triangle、Star。 根据用户在道具中提供的值,我想返回组件。例如,如果用户将道具作为 Rectangle,我想显示 Rectangle 组件。 我不想每次检查所有四个条件时都使用 If-Else 语句。有更好的选择吗?
例如:矩形组件
import React from "react";
function Rectangle(props) {
return (
<div className="term">
<svg width="400" height="110">
<rect
width="300"
height="100"
stroke="black"
stroke-width="3"
/>
</svg>
</div>
);
}
export default Rectangle;
有人可以帮忙吗?提前致谢。
【问题讨论】:
-
如果不想使用
If-Else,可以考虑switch...
标签: reactjs components react-props conditional-rendering