Skip to content

Commit

Permalink
fix: Notification hooks (#24337)
Browse files Browse the repository at this point in the history
* test driven

* use ref for the same content
  • Loading branch information
zombieJ committed May 21, 2020
1 parent f646b6f commit e613861
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
19 changes: 19 additions & 0 deletions components/notification/__tests__/hooks.test.js
Expand Up @@ -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(<Demo />);
});
});
13 changes: 7 additions & 6 deletions components/notification/hooks/useNotification.tsx
Expand Up @@ -46,19 +46,20 @@ export default function createUseNotification(
}

// Fill functions
const hookAPI: any = {
open: notify,
};
const hookApiRef = React.useRef<any>({});

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,
<ConfigConsumer key="holder">
{(context: ConfigConsumerProps) => {
({ getPrefixCls } = context);
Expand Down

0 comments on commit e613861

Please sign in to comment.