useBrowserNotifications: don't set state if already unmounted.
Closes 1053 We can't cancel a promise, but we could prevent setting orphaned states by taking advantage of closures to detect if we have unmounted. Reference: https://juliangaramendy.dev/blog/use-promise-subscription
This commit is contained in:
parent
efbbba6958
commit
d210e81ded
1 changed files with 8 additions and 2 deletions
|
@ -20,13 +20,19 @@ export default () => {
|
|||
|
||||
useEffect(() => {
|
||||
if (!user) return;
|
||||
let mounted = true;
|
||||
setPushSupported(pushNotifications.supported);
|
||||
if (pushNotifications.supported) {
|
||||
pushNotifications.subscribed(user.id).then((isSubscribed: boolean) => {
|
||||
setSubscribed(isSubscribed);
|
||||
setPushInitialized(true);
|
||||
if (mounted) {
|
||||
setSubscribed(isSubscribed);
|
||||
setPushInitialized(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
return () => {
|
||||
mounted = false;
|
||||
};
|
||||
}, [user]);
|
||||
|
||||
useMemo(() => setPushEnabled(pushPermission === 'granted' && subscribed), [pushPermission, subscribed]);
|
||||
|
|
Loading…
Reference in a new issue