javascript - 使用材料 ui 弹出框反应错误边界意外行为
问题描述
弹出框内有一个通知组件,可以进行 fetch 调用。
连接失败,无法访问资源。
错误边界组件包装了通知组件。
预计连接失败后,将显示一个自定义组件,显示出现问题以及重试连接的按钮。
但是,错误边界的行为是:它在弹出框打开时暂停(未显示任何内容),然后在它关闭前几分钟,它显示自定义组件以重试连接并且弹出框完成关闭。
export default function PopOver() {
const [anchorNotificationEl, setAnchorNotificationEl] = React.useState(null);
const popoverNotiRef = React.useRef(null);
const isNotificationOpen = Boolean(anchorNotificationEl);
const handleClick = (event) => {
setAnchorNotificationEl(event.currentTarget);
};
const handleNotificationClose = () => {
setAnchorNotificationEl(null);
};
const notificationId = 'notification-popover';
return (
<>
<IconButtonCustom
aria-label="show 17 new notifications"
color="inherit"
onClick={handleClick}
id="notifications-icon"
>
<Badge
badgeContent={0}
color="secondary"
>
<NotificationsIcon
fontSize="large"
/>
</Badge>
</IconButtonCustom>
<Popover
ref={popoverNotiRef}
id={notificationId}
open={isNotificationOpen}
anchorEl={anchorNotificationEl}
onClose={handleNotificationClose}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'center',
}}
elevation={1}
>
<Notifications popoverRef={popoverNotiRef.current} />
</Popover>
</>
);
}
class ErrorBoundary extends React.Component <Props, State> {
constructor(props) {
super(props);
this.state = { error: null };
}
static getDerivedStateFromError(error): State {
return { error };
}
componentDidCatch(error) {
this.setState({ error });
}
render() {
const { children, fallback } = this.props;
const { error } = this.state;
if (error) {
if (fallback) {
return fallback;
}
return <p>Something is wrong</p>;
}
return children;
}
}
export default function NotificationWrapper({ popoverRef }: NWProps) {
const [queryReference, loadQuery] = useQueryLoader(NotificationsQuery);
useEffect(() => {
loadQuery({
count: 5,
}, { fetchPolicy: 'store-and-network' });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<>
<ErrorBoundary fallback={<RetryComponent loadQuery={loadQuery}/>}>
{queryReference && (
<Suspense fallback={<Loading />}>
<NotificationsIntermediate queryRef={queryReference} popoverRef={popoverRef} />
</Suspense>
)}
</ErrorBoundary>
</>
);
}
解决方案
推荐阅读
- java - 类似于 JPanel 的 JFrame.pack() 的东西
- ssh - Pycharm 无法检测图形环境
- sql - 为什么当数据在表中的后面位置重复时(不在相关行旁边),SQL LAG (HIVE) 给出的差异为 0?
- android - 创建新表时房间迁移查询失败
- html - 如何在网页上发布 HTML 电子邮件?
- javascript - Discord.js SyntaxError: Unexpected Identifier (For command handler)
- sql - 有没有其他方法可以解决涉及员工数据的 SQL 问题?
- mysql - MySQL-Query中的关键字
- python - 附加查询字符串以查看渲染函数 Django
- c - 为什么我的opengl 2D动画代码输出窗口没有响应,只显示动画的最终结果