javascript - 通知被阅读后通知徽章不会消失?
问题描述
这是我在导航栏中的通知图标标记,这不会在阅读通知后删除带有未读通知计数的徽章:
const notificationsIcon =
notifications && notifications.length && notifications.filter(n => !n.read).length ?
<Badge badgeContent={notifications.filter(n => !n.read).length}>
<NotificationsIcon /> //Material-UI Icon
</Badge> : <NotificationsIcon />
但这有效:
let notificationsIcon;
if (notifications && notifications.length > 0) {
notifications.filter(n => n.read === false).length > 0
? (notificationsIcon = (
<Badge
badgeContent={
notifications.filter(n => n.read === false).length
}
>
<NotificationsIcon />
</Badge>
))
: (notificationsIcon = <NotificationsIcon />);
} else {
notificationsIcon = <NotificationsIcon />;
}
我真的不明白为什么第二个标记有效而第一个无效,
const mapStateToProps = state => ({
notifications: state.user.notifications
})
notificationsIcon
变量将包含在带有 Material-UI 元素的 render 方法IconButton
中,单击该元素时会向 redux 存储发送一个操作以将通知read
属性更改为 true,这将更新组件中的通知。
解决方案
我试过这个只是为了测试你的第一个逻辑,这个有效:
let notifications = [{read: true}, {read: true}]
const notificationsIcon =
notifications && notifications.length && notifications.filter(n => !n.read).length ?
"yes" : "no";
console.log(notificationsIcon);
无法尝试使用实际代码,您可以提供 stackblitz 或 plunker。也试试这个:
const notificationsIcon =
notifications && notifications.length && notifications.filter(n => !n.read).length ?
(<Badge badgeContent={notifications.filter(n => !n.read).length}>
<NotificationsIcon /> //Material-UI Icon
</Badge>) : (<NotificationsIcon />);
推荐阅读
- sql - 根据条件重置的 7 天累积总和
- .net - 无法解析符号“ToTable”EF Core 2.2.0
- javascript - 嵌套过滤器数组方法
- postman - 纽曼:ReferenceError:未定义 pm
- ios - Apple 应用内恢复购买:在我允许“恢复购买”请求通过之前,我是否负责验证购买?
- php - 不再能够使用服务帐户通过 Google 日历 API 访问共享日历
- javascript - 在 Wordpress 内联脚本中使用 jQuery
- android - 如何修复 java.lang.NoClassDefFoundError:解析失败:Lcom/android/installreferrer/api/InstallReferrerStateListener;错误
- symfony - 如何修复 Symfony 4 中已弃用的用户?
- angular - Angular 7升级“在'@angular/cdk/stepper'中找不到导出'STEPPER_GLOBAL_OPTIONS'