Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Notification hooks #24337

Merged
merged 2 commits into from May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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