From 641df860f5272ac7a02794ea37fba6b90ff5efb0 Mon Sep 17 00:00:00 2001 From: husseyexplores Date: Fri, 11 Oct 2019 14:22:36 +0500 Subject: [PATCH 1/3] [fixed] Call parent.removeChild only if parent exists --- src/components/Modal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Modal.js b/src/components/Modal.js index 498f8467..14e818c6 100644 --- a/src/components/Modal.js +++ b/src/components/Modal.js @@ -174,7 +174,7 @@ class Modal extends Component { removePortal = () => { !isReact16 && ReactDOM.unmountComponentAtNode(this.node); const parent = getParentElement(this.props.parentSelector); - parent.removeChild(this.node); + parent && parent.removeChild(this.node); }; portalRef = ref => { From 44903862e815cd9495dbc83c8cce5c59c17be01a Mon Sep 17 00:00:00 2001 From: husseyexplores Date: Fri, 11 Oct 2019 17:28:05 +0500 Subject: [PATCH 2/3] Warn if the modal parent is already removed --- src/components/Modal.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/components/Modal.js b/src/components/Modal.js index 14e818c6..0e383784 100644 --- a/src/components/Modal.js +++ b/src/components/Modal.js @@ -174,7 +174,14 @@ class Modal extends Component { removePortal = () => { !isReact16 && ReactDOM.unmountComponentAtNode(this.node); const parent = getParentElement(this.props.parentSelector); - parent && parent.removeChild(this.node); + if (parent) { + parent.removeChild(this.node); + } else { + /* eslint-disable-next-line no-console */ + console.warn( + "Cannot remove modal from the parent element as it no longer exists in the DOM" + ); + } }; portalRef = ref => { From 35aa2ad93a4695b8c16b9ef78a94920daa1e4b5f Mon Sep 17 00:00:00 2001 From: husseyexplores Date: Fri, 11 Oct 2019 18:56:48 +0500 Subject: [PATCH 3/3] Rephrased warning message --- src/components/Modal.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/Modal.js b/src/components/Modal.js index 0e383784..0ef0b618 100644 --- a/src/components/Modal.js +++ b/src/components/Modal.js @@ -177,9 +177,11 @@ class Modal extends Component { if (parent) { parent.removeChild(this.node); } else { - /* eslint-disable-next-line no-console */ + // eslint-disable-next-line no-console console.warn( - "Cannot remove modal from the parent element as it no longer exists in the DOM" + 'React-Modal: "parentSelector" prop did not returned any DOM ' + + "element. Make sure that the parent element is unmounted to " + + "avoid any memory leaks." ); } };