From 8995758b7d9d4564637fac4c4bcc902282729977 Mon Sep 17 00:00:00 2001 From: Maxime Thirouin Date: Tue, 22 Nov 2016 14:22:09 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed:=20Avoid=20catching=20unwa?= =?UTF-8?q?nted=20````=20that=20can=20be=20children=20of=20a=20````=20instance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now we cleanup listened parts of the PageContainer on each mount/update, before re-executing catchLinks. --- src/_utils/catch-links/index.js | 6 +++++- src/components/PageContainer/component.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/_utils/catch-links/index.js b/src/_utils/catch-links/index.js index 710602532..bcd16be2d 100644 --- a/src/_utils/catch-links/index.js +++ b/src/_utils/catch-links/index.js @@ -50,7 +50,11 @@ const catchLinks = (cb) => (ev: any): void => { export default function( elements: Array, cb: Function -): void { +): Function { const eventCallback = catchLinks(cb) elements.map((el) => el.addEventListener("click", eventCallback)) + + return () => { + elements.map((el) => el.removeEventListener("click", eventCallback)) + } } diff --git a/src/components/PageContainer/component.js b/src/components/PageContainer/component.js index 796895373..d105cf8d6 100644 --- a/src/components/PageContainer/component.js +++ b/src/components/PageContainer/component.js @@ -116,6 +116,8 @@ class PageContainer extends Component { this.catchInternalLink() } + cleanupInternalLinks: Function + catchInternalLink() { const layoutDOMElement = findDOMNode(this) @@ -127,6 +129,18 @@ class PageContainer extends Component { if (!bodyContainers.length) { bodyContainers = [ layoutDOMElement ] } + + // as soon as we listened to something, we should carefully unlisten + // because + // - it can be listening in the layoutDOMElement (a parent) + // - it can be listening in a might be catched (but they are not supposed to) + if (this.cleanupInternalLinks) { + this.cleanupInternalLinks() + } + this.cleanupInternalLinks = catchLinks(bodyContainers, (href) => { // do not catch links that are under the current base path if (href.indexOf(process.env.PHENOMIC_USER_PATHNAME) === -1) {