【发布时间】:2021-11-20 22:22:30
【问题描述】:
我有一个组件,我在其中渲染一个 Popover 组件:
// HeaderMenu.tsx
const HeaderMenu = () => {
const ShowMenu = () => {
return (
<div className={classes.profile}>
<ul>
<Notifications />
</ul>
<ul>
<li onClick={handleClose}><Link to={`/profile/${currentUserVar().id}`}>Profile</Link></li>
<li onClick={handleClose}><Link to='/' onClick={logout}>Logout</Link></li>
</ul>
</div>
);
};
return (
<div>
<Button onClick={handleClick}>
<AccountCircleIcon className={classes.profileIcon} />
</Button>
<Popover
id={id}
open={open}
anchorEl={anchorEl}
onClose={handleClose}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
>
<ShowMenu />
</Popover>
</div>
);
};
我可以在ShowMenu 函数中使用onClick={handleClose},但我也有一个嵌套组件:Notifications。在这个组件中,我渲染了一个带有一些链接的列表:
// Notifications.tsx
<Link to={`/profile/${notification.followedUser.id}`}>{notification.followedUser.user_name}</Link>{' '}
如果Notifications 组件中的链接已被点击,我将如何调用HeaderMenu 组件中的handleClose 函数来关闭Popover?
我可以使用 Reactive 变量触发 Notifications 组件的更改(如果已单击链接)并通过调用 handleClose 函数对 HeaderMenu 组件中的更改做出反应。但我希望有更好的方法。有什么建议吗?
【问题讨论】:
标签: reactjs material-ui