reactjs - React with Material UI 模板中索引的某些图标
问题描述
我正在尝试使用if
,因为我想为每个添加一个图标,ListItem
但 React 不允许我这样做,我应该怎么做?我应该改变什么吗?提前致谢。
<List>
{["Inbox", "Starred", "Send email", "Drafts"].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
我尝试使用它,但它没有用:
<ListItemIcon>
{index === 0 <InboxIcon/>}
{index === 1 <MailIcon/>}
{index === 2 <DeleteForeverOutlinedIcon />}
{index === 3 <DeleteTwoToneIcon />}
</ListItemIcon>
解决方案
您的&&
代码中缺少 。你应该写类似
<ListItemIcon>
{index === 0 && <InboxIcon/>}
{index === 1 && <MailIcon/>}
{index === 2 && <DeleteForeverOutlinedIcon />}
{index === 3 && <DeleteTwoToneIcon />}
</ListItemIcon>
推荐阅读
- typescript - TypeScript:作为对象键的功能
- c# - 不匹配的编码将程序梯度生成从 C++ 移植到 C#
- python - 如何从python中的图像中去除白色模糊
- ssl - PWA 能否显示内容存储在另一台服务器上的 iframe(原始域的子域)
- android - DatePickerDialog 和 Theme.MaterialComponents
- javascript - 如何在不干扰滚动位置的情况下更新 html 表格?
- java - 如何在谷歌云 kubernates 中部署 docker 镜像
- flask - 在 VScode 调试器中更改烧瓶运行端口
- dialogflow-es - 如何为在线商店的 Dialogflow 意图构建和部署 JS 文件
- java - 如何创建两个实体并将它们关联起来