Skip to content

Commit

Permalink
[Flight] Set dispatcher for duration of performWork() (facebook#19776)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephsavona authored and koto committed Jun 15, 2021
1 parent 99f70e7 commit 20af7ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -292,8 +290,6 @@ export function resolveModelToJSON(
// Something errored. Don't bother encoding anything up to here.
throw x;
}
} finally {
ReactCurrentDispatcher.current = prevDispatcher;
}
}

Expand Down Expand Up @@ -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++) {
Expand All @@ -364,6 +363,8 @@ function performWork(request: Request): void {
if (request.flowing) {
flushCompletedChunks(request);
}

ReactCurrentDispatcher.current = prevDispatcher;
}

let reentrant = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 20af7ca

Please sign in to comment.