Skip to content

Commit 68aedc6

Browse files
committed
refactor: improve via useEvent (react-component#366)
* revert solution * fix again (cherry picked from commit 3b1e34b)
1 parent 2f5622b commit 68aedc6

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/hooks/useNotification.tsx

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as React from 'react';
33
import type { NotificationsProps, NotificationsRef } from '../Notifications';
44
import Notifications from '../Notifications';
55
import type { OpenConfig, Placement, StackConfig } from '../interface';
6+
import { useEvent } from 'rc-util';
67

78
const defaultGetContainer = () => document.body;
89

@@ -107,34 +108,29 @@ export default function useNotification(
107108

108109
const [taskQueue, setTaskQueue] = React.useState<Task[]>([]);
109110

110-
// memorized shareConfig
111-
const { closeIcon, closable, duration, showProgress, pauseOnHover } = shareConfig;
112-
const memoShareConfig = React.useMemo<typeof shareConfig>(() => {
113-
return {
114-
...shareConfig,
115-
};
116-
}, [closeIcon, closable, duration, showProgress, pauseOnHover]);
111+
const open = useEvent<NotificationAPI['open']>((config) => {
112+
const mergedConfig = mergeConfig(shareConfig, config);
113+
if (mergedConfig.key === null || mergedConfig.key === undefined) {
114+
mergedConfig.key = `rc-notification-${uniqueKey}`;
115+
uniqueKey += 1;
116+
}
117117

118-
// ========================= Refs =========================
119-
const api = React.useMemo<NotificationAPI>(() => {
120-
return {
121-
open: (config) => {
122-
const mergedConfig = mergeConfig(memoShareConfig, config);
123-
if (mergedConfig.key === null || mergedConfig.key === undefined) {
124-
mergedConfig.key = `rc-notification-${uniqueKey}`;
125-
uniqueKey += 1;
126-
}
118+
setTaskQueue((queue) => [...queue, { type: 'open', config: mergedConfig }]);
119+
});
127120

128-
setTaskQueue((queue) => [...queue, { type: 'open', config: mergedConfig }]);
129-
},
121+
// ========================= Refs =========================
122+
const api = React.useMemo<NotificationAPI>(
123+
() => ({
124+
open: open,
130125
close: (key) => {
131126
setTaskQueue((queue) => [...queue, { type: 'close', key }]);
132127
},
133128
destroy: () => {
134129
setTaskQueue((queue) => [...queue, { type: 'destroy' }]);
135130
},
136-
};
137-
}, [memoShareConfig]);
131+
}),
132+
[],
133+
);
138134

139135
// ======================= Container ======================
140136
// React 18 should all in effect that we will check container in each render

0 commit comments

Comments
 (0)