diff --git a/components/notification/__tests__/hooks.test.js b/components/notification/__tests__/hooks.test.js
index b8321246d215..9e88c9254ec3 100644
--- a/components/notification/__tests__/hooks.test.js
+++ b/components/notification/__tests__/hooks.test.js
@@ -50,4 +50,23 @@ describe('notification.hooks', () => {
expect(document.querySelectorAll('.my-test-notification-notice').length).toBe(1);
expect(document.querySelector('.hook-test-result').innerHTML).toEqual('bamboo');
});
+
+ it('should be same hook', () => {
+ let count = 0;
+
+ const Demo = () => {
+ const [, forceUpdate] = React.useState({});
+ const [api] = notification.useNotification();
+
+ React.useEffect(() => {
+ count += 1;
+ expect(count).toEqual(1);
+ forceUpdate();
+ }, [api]);
+
+ return null;
+ };
+
+ mount();
+ });
});
diff --git a/components/notification/hooks/useNotification.tsx b/components/notification/hooks/useNotification.tsx
index ec7110039917..0ba30027cd58 100644
--- a/components/notification/hooks/useNotification.tsx
+++ b/components/notification/hooks/useNotification.tsx
@@ -46,19 +46,20 @@ export default function createUseNotification(
}
// Fill functions
- const hookAPI: any = {
- open: notify,
- };
+ const hookApiRef = React.useRef({});
+
+ hookApiRef.current.open = notify;
+
['success', 'info', 'warning', 'error'].forEach(type => {
- hookAPI[type] = (args: ArgsProps) =>
- hookAPI.open({
+ hookApiRef.current[type] = (args: ArgsProps) =>
+ hookApiRef.current.open({
...args,
type,
});
});
return [
- hookAPI,
+ hookApiRef.current,
{(context: ConfigConsumerProps) => {
({ getPrefixCls } = context);