fix(AbstractNotificationsContainer): dismiss timeout not always set

The docs of 'componentDidUpdate' say that it's not called for the
initial render. If the component is added to the DOM with 1 notification
already, then the update will not happen and timeout will never be set
which will effectively break the timeouts chain.
This commit is contained in:
paweldomas 2019-02-07 16:49:36 -06:00 committed by Zoltan Bettenbuk
parent f3f8dc2072
commit 95f684da2f
1 changed files with 23 additions and 1 deletions

View File

@ -51,18 +51,40 @@ export default class AbstractNotificationsContainer<P: Props>
this._onDismissed = this._onDismissed.bind(this); this._onDismissed = this._onDismissed.bind(this);
} }
/**
* Sets a timeout for the first notification (if applicable).
*
* @inheritdoc
*/
componentDidMount() {
// Set the initial dismiss timeout (if any)
this._manageDismissTimeout();
}
/** /**
* Sets a timeout if the currently displayed notification has changed. * Sets a timeout if the currently displayed notification has changed.
* *
* @inheritdoc * @inheritdoc
*/ */
componentDidUpdate(prevProps: P) { componentDidUpdate(prevProps: P) {
this._manageDismissTimeout(prevProps);
}
/**
* Sets/clears the dismiss timeout for the top notification.
*
* @param {P} [prevProps] - The previous properties (if called from
* {@code componentDidUpdate}).
* @returns {void}
* @private
*/
_manageDismissTimeout(prevProps: ?P) {
const { _notifications } = this.props; const { _notifications } = this.props;
if (_notifications.length) { if (_notifications.length) {
const notification = _notifications[0]; const notification = _notifications[0];
const previousNotification const previousNotification
= prevProps._notifications.length = prevProps && prevProps._notifications.length
? prevProps._notifications[0] ? prevProps._notifications[0]
: undefined; : undefined;