Skip to content

Commit 7415f32

Browse files
fix: avoid unmounted state update in notifications modal
Co-authored-by: Prithpal Sooriya <prithpal.sooriya@gmail.com>
1 parent 90f7bc0 commit 7415f32

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

ui/components/app/modals/turn-on-metamask-notifications/turn-on-metamask-notifications.tsx

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext, useEffect, useState } from 'react';
1+
import React, { useContext, useEffect, useRef, useState } from 'react';
22
import { useSelector } from 'react-redux';
33
import { useNavigate } from 'react-router-dom';
44
import { I18nContext } from '../../../../contexts/i18n';
@@ -57,9 +57,14 @@ export default function TurnOnMetamaskNotifications() {
5757
const [isLoading, setIsLoading] = useState<boolean>(
5858
isUpdatingMetamaskNotifications,
5959
);
60+
const isLoadingRef = useRef(isUpdatingMetamaskNotifications);
6061

6162
const { enableNotifications, error } = useEnableNotifications();
6263

64+
useEffect(() => {
65+
isLoadingRef.current = isLoading;
66+
}, [isLoading]);
67+
6368
const handleTurnOnNotifications = async () => {
6469
setIsLoading(true);
6570
trackEvent({
@@ -78,24 +83,25 @@ export default function TurnOnMetamaskNotifications() {
7883
};
7984

8085
const handleHideModal = () => {
86+
// Read loading state from a ref to avoid scheduling state updates during close.
87+
const isCurrentlyLoading = isLoadingRef.current;
88+
89+
if (!isCurrentlyLoading) {
90+
trackEvent({
91+
category: MetaMetricsEventCategory.NotificationsActivationFlow,
92+
event: MetaMetricsEventName.NotificationsActivated,
93+
properties: {
94+
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
95+
// eslint-disable-next-line @typescript-eslint/naming-convention
96+
is_profile_syncing_enabled: isBackupAndSyncEnabled,
97+
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
98+
// eslint-disable-next-line @typescript-eslint/naming-convention
99+
action_type: 'dismissed',
100+
},
101+
});
102+
}
103+
81104
hideModal();
82-
setIsLoading((prevLoadingState) => {
83-
if (!prevLoadingState) {
84-
trackEvent({
85-
category: MetaMetricsEventCategory.NotificationsActivationFlow,
86-
event: MetaMetricsEventName.NotificationsActivated,
87-
properties: {
88-
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
89-
// eslint-disable-next-line @typescript-eslint/naming-convention
90-
is_profile_syncing_enabled: isBackupAndSyncEnabled,
91-
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
92-
// eslint-disable-next-line @typescript-eslint/naming-convention
93-
action_type: 'dismissed',
94-
},
95-
});
96-
}
97-
return prevLoadingState;
98-
});
99105
};
100106

101107
useEffect(() => {

0 commit comments

Comments
 (0)