From 249c66e717593822b496993b19ffe29d4403ed06 Mon Sep 17 00:00:00 2001 From: zombiej Date: Thu, 21 May 2020 10:18:34 +0800 Subject: [PATCH 1/2] test driven --- .../notification/__tests__/hooks.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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(); + }); }); From 0d205dbe90ba9d2453062b7b1295dd518990f04a Mon Sep 17 00:00:00 2001 From: zombiej Date: Thu, 21 May 2020 10:22:54 +0800 Subject: [PATCH 2/2] use ref for the same content --- components/notification/hooks/useNotification.tsx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) 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);