Skip to content

Commit 28ecc0b

Browse files
mshustovdiasbruno
authored andcommittedJun 13, 2017
[fixed] compatibility with unstable_handleError.
in current React version componentWillUnmount could be called without calling componentDidMount if errors during rendering are handled via unstable_handleError method
1 parent bb15b16 commit 28ecc0b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed
 

‎lib/components/Modal.js

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ var Modal = createReactClass({
8888
},
8989

9090
componentWillUnmount: function() {
91+
if (!this.node) return;
92+
9193
refCount.remove(this);
9294

9395
if (this.props.ariaHideApp) {

‎specs/Modal.spec.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env mocha */
22
import sinon from 'sinon';
33
import expect from 'expect';
4-
import React from 'react';
4+
import React, { Component } from 'react';
55
import ReactDOM from 'react-dom';
66
import TestUtils from 'react-addons-test-utils';
77
import Modal from '../lib/components/Modal';
@@ -308,4 +308,37 @@ describe('State', () => {
308308
done();
309309
}, closeTimeoutMS);
310310
});
311+
312+
it('shouldn\'t throw if forcibly unmounted during mounting', () => {
313+
/* eslint-disable camelcase, react/prop-types */
314+
class Wrapper extends Component {
315+
constructor (props) {
316+
super(props);
317+
this.state = { error: false };
318+
}
319+
unstable_handleError () {
320+
this.setState({ error: true });
321+
}
322+
render () {
323+
return this.state.error ? null : <div>{ this.props.children }</div>;
324+
}
325+
}
326+
/* eslint-enable camelcase, react/prop-types */
327+
328+
const Throw = () => { throw new Error('reason'); };
329+
const TestCase = () => (
330+
<Wrapper>
331+
<Modal />
332+
<Throw />
333+
</Wrapper>
334+
);
335+
336+
const currentDiv = document.createElement('div');
337+
document.body.appendChild(currentDiv);
338+
339+
const mount = () => ReactDOM.render(<TestCase />, currentDiv);
340+
expect(mount).toNotThrow();
341+
342+
document.body.removeChild(currentDiv);
343+
});
311344
});

0 commit comments

Comments
 (0)