Skip to content

Commit 81e41ae

Browse files
committed
Merge pull request facebook#5982 from jimfb/error-boundaries-composite-unmount
Error boundries should not unmount composite components which were not mounted.
2 parents 220b4b6 + 86305fb commit 81e41ae

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/core/__tests__/ReactErrorBoundaries-test.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,40 @@ describe('ReactErrorBoundaries', function() {
7979
expect(EventPluginHub.putListener).toBeCalled();
8080
});
8181

82+
it('correctly handles composite siblings', function() {
83+
class ErrorBoundary extends React.Component {
84+
constructor() {
85+
super();
86+
this.state = {error: false};
87+
}
88+
89+
render() {
90+
if (!this.state.error) {
91+
return <div>{this.props.children}</div>;
92+
}
93+
return <div>Error has been caught</div>;
94+
}
95+
96+
unstable_handleError() {
97+
this.setState({error: true});
98+
}
99+
}
100+
101+
function Broken() {
102+
throw new Error('Always broken.');
103+
}
104+
105+
function Composite() {
106+
return <div />;
107+
}
108+
109+
var container = document.createElement('div');
110+
ReactDOM.render(
111+
<ErrorBoundary><Broken /><Composite /></ErrorBoundary>,
112+
container
113+
);
114+
ReactDOM.unmountComponentAtNode(container);
115+
});
82116

83117
it('catches errors from children', function() {
84118
var log = [];
@@ -144,15 +178,16 @@ describe('ReactErrorBoundaries', function() {
144178
var container = document.createElement('div');
145179
ReactDOM.render(<Box />, container);
146180
expect(container.textContent).toBe('Error: Please, do not render me.');
181+
ReactDOM.unmountComponentAtNode(container);
147182
expect(log).toEqual([
148183
'Box render',
149184
'Inquisitive render',
150185
'Angry render',
151186
'Inquisitive ref null',
152187
'Inquisitive componentWillUnmount',
153-
'Angry componentWillUnmount',
154188
'Box renderError',
155189
'Box componentDidMount',
190+
'Box componentWillUnmount',
156191
]);
157192
});
158193
});

src/renderers/shared/reconciler/ReactCompositeComponent.js

+3
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ var ReactCompositeComponentMixin = {
369369
* @internal
370370
*/
371371
unmountComponent: function() {
372+
if (!this._renderedComponent) {
373+
return;
374+
}
372375
var inst = this._instance;
373376

374377
if (inst.componentWillUnmount) {

0 commit comments

Comments
 (0)