Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/hooks/useNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,19 @@ export default function useNotification(

const [taskQueue, setTaskQueue] = React.useState<Task[]>([]);

// memorized shareConfig
const { closeIcon, closable, duration, showProgress, pauseOnHover } = shareConfig;
const memoShareConfig = React.useMemo<typeof shareConfig>(() => {
return {
...shareConfig,
};
}, [closeIcon, closable, duration, showProgress, pauseOnHover]);
Comment on lines +110 to +116
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closable 类型可以是一个对象,感觉会破坏memo,这里修复方案我感觉可以直接用 rc-util 的 useEvent 就好了。 万金油。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样啊,那有这个hook最好了,又探索到了😀


// ========================= Refs =========================
const api = React.useMemo<NotificationAPI>(() => {
return {
open: (config) => {
const mergedConfig = mergeConfig(shareConfig, config);
const mergedConfig = mergeConfig(memoShareConfig, config);
if (mergedConfig.key === null || mergedConfig.key === undefined) {
mergedConfig.key = `rc-notification-${uniqueKey}`;
uniqueKey += 1;
Expand All @@ -126,7 +134,7 @@ export default function useNotification(
setTaskQueue((queue) => [...queue, { type: 'destroy' }]);
},
};
}, []);
}, [memoShareConfig]);

// ======================= Container ======================
// React 18 should all in effect that we will check container in each render
Expand Down
43 changes: 43 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -849,4 +849,47 @@ describe('Notification.Basic', () => {
expect(document.querySelector('.rc-notification-notice-progress')).toBeFalsy();
});
});

describe('Modifying properties through useState can take effect', () => {
it('should show notification and disappear after 5 seconds', async () => {
const Demo: React.FC = () => {
const [duration, setDuration] = React.useState(0);
const [api, holder] = useNotification({ duration });

return (
<>
<button data-testid="change-duration" onClick={() => setDuration(5)}>
change duration
</button>
<button
data-testid="show-notification"
onClick={() => {
api.open({
content: `Test Notification`,
});
}}
>
show notification
</button>
{holder}
</>
);
};

const { getByTestId } = render(<Demo />);

fireEvent.click(getByTestId('show-notification'));

expect(document.querySelectorAll('.rc-notification-notice').length).toBe(1);
fireEvent.click(getByTestId('change-duration'));
fireEvent.click(getByTestId('show-notification'));
expect(document.querySelectorAll('.rc-notification-notice').length).toBe(2);

act(() => {
vi.advanceTimersByTime(5000);
});

expect(document.querySelectorAll('.rc-notification-notice').length).toBe(1);
});
});
});