@samitouri / QOS-React-2 / commits / 86305fba41

Error boundries should not unmount composite components which were not mounted.

jim committed Feb 4, 2016 at 16:55 UTC 86305fba4112761631ec1d1576bc71204399d6ef
2 files changed +39 -1
src/core/__tests__/ReactErrorBoundaries-test.js
+36 -1
@@ -79,6 +79,40 @@ describe('ReactErrorBoundaries', function() {
79 expect(EventPluginHub.putListener).toBeCalled();
80 });
81
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 + });
116
117 it('catches errors from children', function() {
118 var log = [];
@@ -144,15 +178,16 @@ describe('ReactErrorBoundaries', function() {
178 var container = document.createElement('div');
179 ReactDOM.render(<Box />, container);
180 expect(container.textContent).toBe('Error: Please, do not render me.');
181 + ReactDOM.unmountComponentAtNode(container);
182 expect(log).toEqual([
183 'Box render',
184 'Inquisitive render',
185 'Angry render',
186 'Inquisitive ref null',
187 'Inquisitive componentWillUnmount',
153 - 'Angry componentWillUnmount',
188 'Box renderError',
189 'Box componentDidMount',
190 + 'Box componentWillUnmount',
191 ]);
192 });
193 });
src/renderers/shared/reconciler/ReactCompositeComponent.js
+3
@@ -369,6 +369,9 @@ var ReactCompositeComponentMixin = {
369 * @internal
370 */
371 unmountComponent: function() {
372 + if (!this._renderedComponent) {
373 + return;
374 + }
375 var inst = this._instance;
376
377 if (inst.componentWillUnmount) {