[英]How to add another prop to my Switch statement in React app
I have a React component based on a type
prop I am returning jsx.我有一个基于
type
道具的 React 组件,我正在返回 jsx。
type Props = {
variant: 'type-one' | 'type-two' | 'type-three';
show: boolean;
};
const Placeholder = ({ type, show }: Props) => {
switch (variant) {
case 'type-one':
return (
<div id="type-one" className="type-one" />
);
case 'type-two':
return (
<div id="type-two" className="type-two" />
);
case 'type-three':
return (
<div id="type-three" className="type-three" />
);
default:
return null;
}
};
export default Placeholder;
How do I add the show
prop here in a clean way?如何以干净的方式在此处添加
show
道具? When show === true
I want to show the specific <div>
otherwise return null
(nothing)当
show === true
我想显示特定的<div>
否则返回null
(什么都没有)
This is how the switch case works:这是开关盒的工作原理:
const Placeholder = ({ variant, show }: Props) => {
if(show){
switch (variant) {
case 'type-one':
return (
<div id="type-one" className="type-one" />
);
case 'type-two':
return (
<div id="type-two" className="type-two" />
);
case 'type-three':
return (
<div id="type-three" className="type-three" />
);
default:
return null;
}
}
else {
return null;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.