mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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:
+5
-4
@@ -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(
|
||||
|
||||
+22
@@ -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!');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user