【发布时间】:2021-10-16 11:07:22
【问题描述】:
包含 ClickAwayListener 的 Popper,触发子组件的所有 onClick 函数。防止这种情况的正确方法是什么?如果您需要更多信息,请告诉我!
父代码:
<Popper
open={Boolean(this.state.openNotification)}
anchorEl={this.state.openNotification}
placement='bottom-end'
disablePortal={true}
transition
>
{({TransitionProps, placement}) => (
<Grow {...TransitionProps} id="notification-menu-list-grow">
<Paper className={classes.notificationsPaper}>
<ClickAwayListener onClickAway={this.handleCloseNotification.bind(this)}>
{this.state.notificationList.length > 0 ?
<List>
{
this.state.notificationList.map((notification, index) =>
<NotificationListItem key={index} id={notification._id}
type={notification.type}
title={notification.title}
read={notification.read}
timestamp={notification.timestamp}/>)
}
</List>
:
<Typography className={classes.noNotifText}>Keine Benachrichtigungen</Typography>
}
</ClickAwayListener>
<IconButton edge="end" aria-label="delete all" className={classes.deleteAllIConButton} onClick={this.deleteAllNotifications()}>
<ClearAllIcon/>
</IconButton>
</Paper>
</Grow>
)}
</Popper>
子组件:
return (
<ListItem id={props.id}>
<ListItemText className={!props.read ? classes.notificationHeaderTitleUnread : classes.notificationHeaderTitle}
primary={props.title} secondary={props.timestamp}/>
<ListItemSecondaryAction>
<IconButton edge="end" aria-label="delete" onClick={deleteNotification()}>
<DeleteOutlineIcon/>
</IconButton>
</ListItemSecondaryAction>
</ListItem>
);
【问题讨论】:
-
你的onClick不应该像
onClick={() => deleteNotification()}吗? -
和那个一起工作,它们之间有什么区别?
标签: reactjs material-ui