Przeglądaj źródła

fix: fix NoticeIcon state update on an unmounted component (#8726)

陈帅 4 lat temu
rodzic
commit
64c4f05cb4
1 zmienionych plików z 4 dodań i 3 usunięć
  1. 4 3
      src/components/NoticeIcon/index.tsx

+ 4 - 3
src/components/NoticeIcon/index.tsx

@@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
 import { Tag, message } from 'antd';
 import { groupBy } from 'lodash';
 import moment from 'moment';
-import { useModel } from 'umi';
+import { useModel, useRequest } from 'umi';
 import { getNotices } from '@/services/ant-design-pro/api';
 
 import NoticeIcon from './NoticeIcon';
@@ -74,10 +74,11 @@ const NoticeIconView = () => {
   const { initialState } = useModel('@@initialState');
   const { currentUser } = initialState || {};
   const [notices, setNotices] = useState<API.NoticeIconItem[]>([]);
+  const { data } = useRequest(getNotices());
 
   useEffect(() => {
-    getNotices().then(({ data }) => setNotices(data || []));
-  }, []);
+    setNotices(data || []);
+  }, [data]);
 
   const noticeData = getNoticeData(notices);
   const unreadMsg = getUnreadData(noticeData || {});