[英]How to add another prop to my Switch statement in React app
我有一個基於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;
如何以干凈的方式在此處添加show
道具? 當show === true
我想顯示特定的<div>
否則返回null
(什么都沒有)
這是開關盒的工作原理:
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.