[Flight] Set dispatcher for duration of performWork() (#19776)

This commit is contained in:
Joseph Savona 2020-09-07 19:41:22 -04:00 committed by GitHub
parent 4f3f7eeb7f
commit d38ec17b1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View File

@ -273,11 +273,9 @@ export function resolveModelToJSON(
value !== null &&
value.$$typeof === REACT_ELEMENT_TYPE
) {
const prevDispatcher = ReactCurrentDispatcher.current;
// TODO: Concatenate keys of parents onto children.
const element: React$Element<any> = (value: any);
try {
ReactCurrentDispatcher.current = Dispatcher;
// Attempt to render the server component.
value = attemptResolveElement(element);
} catch (x) {
@ -292,8 +290,6 @@ export function resolveModelToJSON(
// Something errored. Don't bother encoding anything up to here.
throw x;
}
} finally {
ReactCurrentDispatcher.current = prevDispatcher;
}
}
@ -355,6 +351,9 @@ function retrySegment(request: Request, segment: Segment): void {
}
function performWork(request: Request): void {
const prevDispatcher = ReactCurrentDispatcher.current;
ReactCurrentDispatcher.current = Dispatcher;
const pingedSegments = request.pingedSegments;
request.pingedSegments = [];
for (let i = 0; i < pingedSegments.length; i++) {
@ -364,6 +363,8 @@ function performWork(request: Request): void {
if (request.flowing) {
flushCompletedChunks(request);
}
ReactCurrentDispatcher.current = prevDispatcher;
}
let reentrant = false;

View File

@ -213,4 +213,22 @@ describe('ReactFlightDOMRelay', () => {
},
});
});
it('can handle a subset of Hooks, with element as root', () => {
const {useMemo, useCallback} = React;
function Inner({x}) {
const foo = useMemo(() => x + x, [x]);
const bar = useCallback(() => 10 + foo, [foo]);
return bar();
}
function Foo() {
return <Inner x={2} />;
}
const transport = [];
ReactDOMFlightRelayServer.render(<Foo />, transport);
const model = readThrough(transport);
expect(model).toEqual(14);
});
});