Skip to content

Commit ea5aaf0

Browse files
committed
[fixed] Prevent unintended register and unregister
- Fixed [reactjs#808](reactjs#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`.
1 parent a275399 commit ea5aaf0

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/components/ModalPortal.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default class ModalPortal extends Component {
133133
}
134134

135135
componentWillUnmount() {
136-
if (this.state.isOpen) {
136+
if (this.props.isOpen) {
137137
this.afterClose();
138138
}
139139
clearTimeout(this.closeTimer);
@@ -227,11 +227,11 @@ export default class ModalPortal extends Component {
227227
};
228228

229229
open = () => {
230-
this.beforeOpen();
231230
if (this.state.afterOpen && this.state.beforeClose) {
232231
clearTimeout(this.closeTimer);
233232
this.setState({ beforeClose: false });
234233
} else {
234+
this.beforeOpen();
235235
if (this.props.shouldFocusAfterRender) {
236236
focusManager.setupScopedFocus(this.node);
237237
focusManager.markForFocusLater();
@@ -268,24 +268,21 @@ export default class ModalPortal extends Component {
268268

269269
closeWithTimeout = () => {
270270
const closesAt = Date.now() + this.props.closeTimeoutMS;
271-
this.setState({ beforeClose: true, closesAt }, () => {
272-
this.closeTimer = setTimeout(
273-
this.closeWithoutTimeout,
274-
this.state.closesAt - Date.now()
275-
);
276-
});
271+
this.setState({ beforeClose: true, closesAt });
272+
this.closeTimer = setTimeout(
273+
this.closeWithoutTimeout,
274+
this.props.closeTimeoutMS
275+
);
277276
};
278277

279278
closeWithoutTimeout = () => {
280-
this.setState(
281-
{
282-
beforeClose: false,
283-
isOpen: false,
284-
afterOpen: false,
285-
closesAt: null
286-
},
287-
this.afterClose
288-
);
279+
this.setState({
280+
beforeClose: false,
281+
isOpen: false,
282+
afterOpen: false,
283+
closesAt: null
284+
});
285+
this.afterClose();
289286
};
290287

291288
handleKeyDown = event => {

0 commit comments

Comments
 (0)