Handle errors thrown in gDSFP of a module-style context provider (#13269)

Context should be pushed before calling any user code, so if it errors
the stack unwinds correctly.
This commit is contained in:
Andrew Clark
2018-07-25 14:22:27 -07:00
committed by GitHub
parent 0154a79fed
commit bc1ea9cd96
2 changed files with 27 additions and 4 deletions
+5 -4
View File
@@ -618,6 +618,11 @@ function mountIndeterminateComponent(
// Proceed under the assumption that this is a class instance
workInProgress.tag = ClassComponent;
// Push context providers early to prevent context stack mismatches.
// During mounting we don't know the child context yet as the instance doesn't exist.
// We will invalidate the child context in finishClassComponent() right after rendering.
const hasContext = pushLegacyContextProvider(workInProgress);
workInProgress.memoizedState =
value.state !== null && value.state !== undefined ? value.state : null;
@@ -630,10 +635,6 @@ function mountIndeterminateComponent(
);
}
// Push context providers early to prevent context stack mismatches.
// During mounting we don't know the child context yet as the instance doesn't exist.
// We will invalidate the child context in finishClassComponent() right after rendering.
const hasContext = pushLegacyContextProvider(workInProgress);
adoptClassInstance(workInProgress, value);
mountClassInstance(workInProgress, renderExpirationTime);
return finishClassComponent(
@@ -1469,4 +1469,26 @@ describe('ReactIncrementalErrorHandling', () => {
ReactNoop.flushDeferredPri();
expect(ReactNoop.getChildren()).toEqual([span('Caught an error: Hello')]);
});
it('handles error thrown inside getDerivedStateFromProps of a module-style context provider', () => {
function Provider() {
return {
getChildContext() {
return {foo: 'bar'};
},
render() {
return 'Hi';
},
};
}
Provider.childContextTypes = {
x: () => {},
};
Provider.getDerivedStateFromProps = () => {
throw new Error('Oops!');
};
ReactNoop.render(<Provider />);
expect(() => ReactNoop.flush()).toThrow('Oops!');
});
});