reactjs - 如何检查一个id是否等于用户的id并在react js中有条件地显示按钮?
问题描述
嘿伙计们,我是新手,目前正在尝试根据条件显示 2 个按钮。我需要检查用户是否存在于具有用户 ID 的数组中并显示相应的按钮。
这是我收到的 json 响应。
(2) [{…}, {…}]
0: {id: 8, username: "testuser5"}
1: {id: 4, username: "testuser2"}
length: 2
__proto__: Array(0)
这是我的代码。
const [pending, setPending] = useState([]);
const pendingfun = async function pendingInfo() {
await axiosInstance.get(url).then((res) => {
const pending = res.data;
setPending(pending)
// console.log(pending)
})
}
pending.forEach(x => x.id === user.id) ?
<Button variant="outlined" color="primary" disableElevation
>
Save
</Button>
: <Button variant="outlined" color="primary" disableElevation>
Cancel
</Button>
问题是当我运行它时,屏幕上没有显示任何按钮。有人能帮忙吗?
谢谢
解决方案
推荐阅读
- sql - 如何从 Metabase 中的 .sql 文件创建数据库
- next.js - 在 Next.js 的 SSR 期间 Vercel 使用什么时区?
- php - PHP 输出菜单项链接:要转换为 json 的数组
- python - 如何在 django 中使用 JsonField 而不是多对多字段
- vba - 在文本框中打印数组
- android - Jetpack Compose:列表更改时更新可组合
- javascript - 选择子类别和自动完成
- web-scraping - 使用 BS4 抓取 Shopee 评论和评级
- c# - 如何在 C# 中打开现有表单?
- dns - 我们可以将多个域名指向同一个 IP 地址吗?