From 4a26029975d9f14c754ea354a00ea0b135de734e Mon Sep 17 00:00:00 2001 From: anluu Date: Mon, 28 Oct 2024 17:56:08 +0700 Subject: [PATCH] fix: keep add/remove item to notifications list consistent --- src/NotificationSystem.jsx | 49 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/NotificationSystem.jsx b/src/NotificationSystem.jsx index 243438f..2455c07 100644 --- a/src/NotificationSystem.jsx +++ b/src/NotificationSystem.jsx @@ -104,20 +104,22 @@ class NotificationSystem extends React.Component { _didNotificationRemoved(uid) { var notification; - var notifications = this.state.notifications.filter(function(toCheck) { - if (toCheck.uid === uid) { - notification = toCheck; - return false; - } - return true; - }); - if (this._isMounted) { - this.setState({ notifications: notifications }); - } + this.setState(prevState => { + var notifications = prevState.notifications.filter(function(toCheck) { + if (toCheck.uid === uid) { + notification = toCheck; + return false; + } + return true; + }); - if (notification && notification.onRemove) { - notification.onRemove(notification); + return { notifications }; + }, () => { + if (notification && notification.onRemove) { + notification.onRemove(notification); + } + }); } } @@ -163,19 +165,16 @@ class NotificationSystem extends React.Component { } } - if (this.props.newOnTop) { - notifications.unshift(_notification); - } else { - notifications.push(_notification); - } - - - if (typeof _notification.onAdd === 'function') { - notification.onAdd(_notification); - } - - this.setState({ - notifications: notifications + this.setState(prevState => { + const otherNotifications = prevState.notifications.filter(item => item.uid !== _notification.uid); + return { + notifications: + this.props.newOnTop ? [_notification].concat(otherNotifications) : otherNotifications.concat([_notification]) + }; + }, () => { + if (typeof _notification.onAdd === 'function') { + notification.onAdd(_notification); + } }); return _notification;