From e613861e6335729cadb18dc1eb934372161d8ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Thu, 21 May 2020 10:36:18 +0800 Subject: [PATCH] fix: Notification hooks (#24337) * test driven * use ref for the same content --- .../notification/__tests__/hooks.test.js | 19 +++++++++++++++++++ .../notification/hooks/useNotification.tsx | 13 +++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) 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);