Skip to content

Commit d315300

Browse files
authored
fix: global duration not work(Also includes some other attributes) (#365)
* fix: some properties changed by useState are not work * test: add test
1 parent f4c10c1 commit d315300

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/hooks/useNotification.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,19 @@ export default function useNotification(
107107

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

110+
// memorized shareConfig
111+
const { closeIcon, closable, duration, showProgress, pauseOnHover } = shareConfig;
112+
const memoShareConfig = React.useMemo<typeof shareConfig>(() => {
113+
return {
114+
...shareConfig,
115+
};
116+
}, [closeIcon, closable, duration, showProgress, pauseOnHover]);
117+
110118
// ========================= Refs =========================
111119
const api = React.useMemo<NotificationAPI>(() => {
112120
return {
113121
open: (config) => {
114-
const mergedConfig = mergeConfig(shareConfig, config);
122+
const mergedConfig = mergeConfig(memoShareConfig, config);
115123
if (mergedConfig.key === null || mergedConfig.key === undefined) {
116124
mergedConfig.key = `rc-notification-${uniqueKey}`;
117125
uniqueKey += 1;
@@ -126,7 +134,7 @@ export default function useNotification(
126134
setTaskQueue((queue) => [...queue, { type: 'destroy' }]);
127135
},
128136
};
129-
}, []);
137+
}, [memoShareConfig]);
130138

131139
// ======================= Container ======================
132140
// React 18 should all in effect that we will check container in each render

tests/index.test.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,4 +849,47 @@ describe('Notification.Basic', () => {
849849
expect(document.querySelector('.rc-notification-notice-progress')).toBeFalsy();
850850
});
851851
});
852+
853+
describe('Modifying properties through useState can take effect', () => {
854+
it('should show notification and disappear after 5 seconds', async () => {
855+
const Demo: React.FC = () => {
856+
const [duration, setDuration] = React.useState(0);
857+
const [api, holder] = useNotification({ duration });
858+
859+
return (
860+
<>
861+
<button data-testid="change-duration" onClick={() => setDuration(5)}>
862+
change duration
863+
</button>
864+
<button
865+
data-testid="show-notification"
866+
onClick={() => {
867+
api.open({
868+
content: `Test Notification`,
869+
});
870+
}}
871+
>
872+
show notification
873+
</button>
874+
{holder}
875+
</>
876+
);
877+
};
878+
879+
const { getByTestId } = render(<Demo />);
880+
881+
fireEvent.click(getByTestId('show-notification'));
882+
883+
expect(document.querySelectorAll('.rc-notification-notice').length).toBe(1);
884+
fireEvent.click(getByTestId('change-duration'));
885+
fireEvent.click(getByTestId('show-notification'));
886+
expect(document.querySelectorAll('.rc-notification-notice').length).toBe(2);
887+
888+
act(() => {
889+
vi.advanceTimersByTime(5000);
890+
});
891+
892+
expect(document.querySelectorAll('.rc-notification-notice').length).toBe(1);
893+
});
894+
});
852895
});

0 commit comments

Comments
 (0)