From 80a18f9e7408283a4f57f0919d3397145925fc08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tibor=20Kranj=C4=8Dec?= Date: Thu, 1 Feb 2018 11:16:09 +0100 Subject: [PATCH] Update notificationsEvents.md Changes the documentation so the example is correct in the prevention of the memory leaks. `.bind` will return a new version of the function on each call which means `removeEventListener` won't be able to find the previous listener and release it. --- docs/notificationsEvents.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/notificationsEvents.md b/docs/notificationsEvents.md index 23a76a0..13e021b 100644 --- a/docs/notificationsEvents.md +++ b/docs/notificationsEvents.md @@ -14,9 +14,13 @@ Example: ```javascript constructor() { - NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this)); - NotificationsIOS.addEventListener('notificationReceivedBackground', this.onNotificationReceivedBackground.bind(this)); - NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this)); + this._boundOnNotificationReceivedForeground = this.onNotificationReceivedForeground.bind(this); + this._boundOnNotificationReceivedBackground = this.onNotificationReceivedBackground.bind(this); + this._boundOnNotificationOpened = this.onNotificationOpened.bind(this); + + NotificationsIOS.addEventListener('notificationReceivedForeground', this._boundOnNotificationReceivedForeground); + NotificationsIOS.addEventListener('notificationReceivedBackground', this._boundOnNotificationReceivedBackground); + NotificationsIOS.addEventListener('notificationOpened', this._boundOnNotificationOpened); } onNotificationReceivedForeground(notification) { @@ -33,9 +37,9 @@ onNotificationOpened(notification) { componentWillUnmount() { // Don't forget to remove the event listeners to prevent memory leaks! - NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this)); - NotificationsIOS.removeEventListener('notificationReceivedBackground', this.onNotificationReceivedBackground.bind(this)); - NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this)); + NotificationsIOS.removeEventListener('notificationReceivedForeground', this._boundOnNotificationReceivedForeground); + NotificationsIOS.removeEventListener('notificationReceivedBackground', this._boundOnNotificationReceivedBackground); + NotificationsIOS.removeEventListener('notificationOpened', this._boundOnNotificationOpened); } ``` -- 2.26.2