From ea5aaf0815ad9f4884821102cf3d0f160711e8f9 Mon Sep 17 00:00:00 2001 From: maro Date: Wed, 14 Feb 2024 03:27:31 +0900 Subject: [PATCH] [fixed] Prevent unintended register and unregister - Fixed [reactjs/react-modal#808](https://github.com/reactjs/react-modal/issues/808) - Checking `this.props.isOpen` instead of `this.state.isOpen` in `componentWillUnmount`. - Moved the call to `this.beforeOpen()` from the beginning of the `open` method to an else block. - Set `this.closeTimer` without waiting until the end of the setState. - Call `this.afterClose()` directly after setting the state in `closeWithoutTimeout`. --- src/components/ModalPortal.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/components/ModalPortal.js b/src/components/ModalPortal.js index 7f20df67..2a977079 100644 --- a/src/components/ModalPortal.js +++ b/src/components/ModalPortal.js @@ -133,7 +133,7 @@ export default class ModalPortal extends Component { } componentWillUnmount() { - if (this.state.isOpen) { + if (this.props.isOpen) { this.afterClose(); } clearTimeout(this.closeTimer); @@ -227,11 +227,11 @@ export default class ModalPortal extends Component { }; open = () => { - this.beforeOpen(); if (this.state.afterOpen && this.state.beforeClose) { clearTimeout(this.closeTimer); this.setState({ beforeClose: false }); } else { + this.beforeOpen(); if (this.props.shouldFocusAfterRender) { focusManager.setupScopedFocus(this.node); focusManager.markForFocusLater(); @@ -268,24 +268,21 @@ export default class ModalPortal extends Component { closeWithTimeout = () => { const closesAt = Date.now() + this.props.closeTimeoutMS; - this.setState({ beforeClose: true, closesAt }, () => { - this.closeTimer = setTimeout( - this.closeWithoutTimeout, - this.state.closesAt - Date.now() - ); - }); + this.setState({ beforeClose: true, closesAt }); + this.closeTimer = setTimeout( + this.closeWithoutTimeout, + this.props.closeTimeoutMS + ); }; closeWithoutTimeout = () => { - this.setState( - { - beforeClose: false, - isOpen: false, - afterOpen: false, - closesAt: null - }, - this.afterClose - ); + this.setState({ + beforeClose: false, + isOpen: false, + afterOpen: false, + closesAt: null + }); + this.afterClose(); }; handleKeyDown = event => {