【发布时间】:2022-01-04 00:28:40
【问题描述】:
我正在映射一组项目。有些项目必须显示视频,而其他项目必须显示图像。我为每个显示器制作了 2 个函数,并使用 useState 来切换它们。
export default function App() {
//SIMPLE USESTATE TO TOGGLE WINDOW
const [open, setOpen] = useState(false);
//THE ARRAY (1 contains an image and the other a video)
const itemsArray = [
{
name: "Item1",
imageUrl: "some image url"
},
{
name: "Item2",
videoUrl: "some video url"
}
];
//RENDER THIS IF ITEM IS IMAGE
const ifImage = (i) => {
return (
<div onClick={() => setOpen(!open)}>
<img src={i} alt="cat" />
</div>
);
};
//RENDER THIS IF ITEM IS VIDEO
const ifVideo = (v) => {
return (
<div className="window-on-top" onClick={() => setOpen(!open)}>
<iframe>Some Video</iframe>
</div>
);
};
return (
<div className="App">
<h3>One button shows a cat photo and the other a cat video</h3>
{itemsArray.map((item) => {
return (
<div key={item.name}>
<button className="niceBtn" onClick={() => setOpen(!open)}>
{item.name}
</button>
{/* NESTING CONDITIONALS OR SOMETHING TO MAKE THIS WORK */}
{open ? {
{item.imageUrl ? ifImage(item.imageUrl): null}
||
{item.videoUrl ? ifVideo(item.videoUrl): null}
} : null}
</div>
);
})}
</div>
);
}
我显然错了...需要一些帮助来了解如何解决这个问题... 这是一个沙盒,其中包含正确观看它的代码。 SANDBOX
【问题讨论】:
标签: arrays reactjs use-state conditional-operator multiple-conditions