Skip to content

Commit 19ef0ec

Browse files
authored
Separate current owner and dispatcher (#14548)
1 parent a9b035b commit 19ef0ec

12 files changed

+56
-32
lines changed

packages/react-cache/src/ReactCache.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ const Pending = 0;
4646
const Resolved = 1;
4747
const Rejected = 2;
4848

49-
const currentOwner =
50-
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner;
49+
const ReactCurrentDispatcher =
50+
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
51+
.ReactCurrentDispatcher;
5152

5253
function readContext(Context, observedBits) {
53-
const dispatcher = currentOwner.currentDispatcher;
54+
const dispatcher = ReactCurrentDispatcher.current;
5455
if (dispatcher === null) {
5556
throw new Error(
5657
'react-cache: read and preload may only be called from within a ' +

packages/react-debug-tools/src/ReactDebugHooks.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
ForwardRef,
2121
} from 'shared/ReactWorkTags';
2222

23-
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
23+
const ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
2424

2525
// Used to track hooks called during a render
2626

@@ -409,17 +409,17 @@ export function inspectHooks<Props>(
409409
renderFunction: Props => React$Node,
410410
props: Props,
411411
): HooksTree {
412-
let previousDispatcher = ReactCurrentOwner.currentDispatcher;
412+
let previousDispatcher = ReactCurrentDispatcher.current;
413413
let readHookLog;
414-
ReactCurrentOwner.currentDispatcher = Dispatcher;
414+
ReactCurrentDispatcher.current = Dispatcher;
415415
let ancestorStackError;
416416
try {
417417
ancestorStackError = new Error();
418418
renderFunction(props);
419419
} finally {
420420
readHookLog = hookLog;
421421
hookLog = [];
422-
ReactCurrentOwner.currentDispatcher = previousDispatcher;
422+
ReactCurrentDispatcher.current = previousDispatcher;
423423
}
424424
let rootStack = ErrorStackParser.parse(ancestorStackError);
425425
return buildTree(rootStack, readHookLog);
@@ -451,17 +451,17 @@ function inspectHooksOfForwardRef<Props, Ref>(
451451
props: Props,
452452
ref: Ref,
453453
): HooksTree {
454-
let previousDispatcher = ReactCurrentOwner.currentDispatcher;
454+
let previousDispatcher = ReactCurrentDispatcher.current;
455455
let readHookLog;
456-
ReactCurrentOwner.currentDispatcher = Dispatcher;
456+
ReactCurrentDispatcher.current = Dispatcher;
457457
let ancestorStackError;
458458
try {
459459
ancestorStackError = new Error();
460460
renderFunction(props, ref);
461461
} finally {
462462
readHookLog = hookLog;
463463
hookLog = [];
464-
ReactCurrentOwner.currentDispatcher = previousDispatcher;
464+
ReactCurrentDispatcher.current = previousDispatcher;
465465
}
466466
let rootStack = ErrorStackParser.parse(ancestorStackError);
467467
return buildTree(rootStack, readHookLog);

packages/react-dom/src/__tests__/ReactServerRendering-test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
let React;
1414
let ReactDOMServer;
1515
let PropTypes;
16-
let ReactCurrentOwner;
16+
let ReactCurrentDispatcher;
1717

1818
function normalizeCodeLocInfo(str) {
1919
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
@@ -25,9 +25,9 @@ describe('ReactDOMServer', () => {
2525
React = require('react');
2626
PropTypes = require('prop-types');
2727
ReactDOMServer = require('react-dom/server');
28-
ReactCurrentOwner =
28+
ReactCurrentDispatcher =
2929
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
30-
.ReactCurrentOwner;
30+
.ReactCurrentDispatcher;
3131
});
3232

3333
describe('renderToString', () => {
@@ -439,7 +439,7 @@ describe('ReactDOMServer', () => {
439439
const Context = React.createContext(0);
440440

441441
function readContext(context) {
442-
return ReactCurrentOwner.currentDispatcher.readContext(context);
442+
return ReactCurrentDispatcher.current.readContext(context);
443443
}
444444

445445
function Consumer(props) {

packages/react-dom/src/server/ReactPartialRenderer.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const toArray = ((React.Children.toArray: any): toArrayType);
8787
// Each stack is an array of frames which may contain nested stacks of elements.
8888
let currentDebugStacks = [];
8989

90-
let ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
90+
let ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
9191
let ReactDebugCurrentFrame;
9292
let prevGetCurrentStackImpl = null;
9393
let getCurrentServerStackImpl = () => '';
@@ -785,11 +785,11 @@ class ReactDOMServerRenderer {
785785

786786
const prevThreadID = currentThreadID;
787787
setCurrentThreadID(this.threadID);
788-
const prevDispatcher = ReactCurrentOwner.currentDispatcher;
788+
const prevDispatcher = ReactCurrentDispatcher.current;
789789
if (enableHooks) {
790-
ReactCurrentOwner.currentDispatcher = Dispatcher;
790+
ReactCurrentDispatcher.current = Dispatcher;
791791
} else {
792-
ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
792+
ReactCurrentDispatcher.current = DispatcherWithoutHooks;
793793
}
794794
try {
795795
// Markup generated within <Suspense> ends up buffered until we know
@@ -870,7 +870,7 @@ class ReactDOMServerRenderer {
870870
}
871871
return out[0];
872872
} finally {
873-
ReactCurrentOwner.currentDispatcher = prevDispatcher;
873+
ReactCurrentDispatcher.current = prevDispatcher;
874874
setCurrentThreadID(prevThreadID);
875875
}
876876
}

packages/react-reconciler/src/ReactFiberClassComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ import {
5555
flushPassiveEffects,
5656
} from './ReactFiberScheduler';
5757

58-
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
58+
const ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
5959

6060
function readContext(contextType: any): any {
61-
const dispatcher = ReactCurrentOwner.currentDispatcher;
61+
const dispatcher = ReactCurrentDispatcher.current;
6262
return dispatcher.readContext(contextType);
6363
}
6464

packages/react-reconciler/src/ReactFiberScheduler.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export type Thenable = {
171171
then(resolve: () => mixed, reject?: () => mixed): mixed,
172172
};
173173

174-
const {ReactCurrentOwner} = ReactSharedInternals;
174+
const {ReactCurrentDispatcher, ReactCurrentOwner} = ReactSharedInternals;
175175

176176
let didWarnAboutStateTransition;
177177
let didWarnSetStateChildContext;
@@ -1209,9 +1209,9 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
12091209

12101210
isWorking = true;
12111211
if (enableHooks) {
1212-
ReactCurrentOwner.currentDispatcher = Dispatcher;
1212+
ReactCurrentDispatcher.current = Dispatcher;
12131213
} else {
1214-
ReactCurrentOwner.currentDispatcher = DispatcherWithoutHooks;
1214+
ReactCurrentDispatcher.current = DispatcherWithoutHooks;
12151215
}
12161216

12171217
const expirationTime = root.nextExpirationTimeToWorkOn;
@@ -1373,7 +1373,7 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
13731373

13741374
// We're done performing work. Time to clean up.
13751375
isWorking = false;
1376-
ReactCurrentOwner.currentDispatcher = null;
1376+
ReactCurrentDispatcher.current = null;
13771377
resetContextDependences();
13781378
resetHooks();
13791379

packages/react-reconciler/src/__tests__/ReactMemo-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('memo', () => {
136136
function readContext(Context) {
137137
const dispatcher =
138138
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
139-
.ReactCurrentOwner.currentDispatcher;
139+
.ReactCurrentDispatcher.current;
140140
return dispatcher.readContext(Context);
141141
}
142142

packages/react-reconciler/src/__tests__/ReactNewContext-test.internal.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ describe('ReactNewContext', () => {
3939

4040
function readContext(Context, observedBits) {
4141
const dispatcher =
42-
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner
43-
.currentDispatcher;
42+
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
43+
.ReactCurrentDispatcher.current;
4444
return dispatcher.readContext(Context, observedBits);
4545
}
4646

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import typeof {Dispatcher} from 'react-reconciler/src/ReactFiberDispatcher';
11+
12+
/**
13+
* Keeps track of the current dispatcher.
14+
*/
15+
const ReactCurrentDispatcher = {
16+
/**
17+
* @internal
18+
* @type {ReactComponent}
19+
*/
20+
current: (null: null | Dispatcher),
21+
};
22+
23+
export default ReactCurrentDispatcher;

packages/react/src/ReactCurrentOwner.js

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import type {Fiber} from 'react-reconciler/src/ReactFiber';
11-
import typeof {Dispatcher} from 'react-reconciler/src/ReactFiberDispatcher';
1211

1312
/**
1413
* Keeps track of the current owner.
@@ -22,7 +21,6 @@ const ReactCurrentOwner = {
2221
* @type {ReactComponent}
2322
*/
2423
current: (null: null | Fiber),
25-
currentDispatcher: (null: null | Dispatcher),
2624
};
2725

2826
export default ReactCurrentOwner;

packages/react/src/ReactHooks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import type {ReactContext} from 'shared/ReactTypes';
1111
import invariant from 'shared/invariant';
1212
import warning from 'shared/warning';
1313

14-
import ReactCurrentOwner from './ReactCurrentOwner';
14+
import ReactCurrentDispatcher from './ReactCurrentDispatcher';
1515

1616
function resolveDispatcher() {
17-
const dispatcher = ReactCurrentOwner.currentDispatcher;
17+
const dispatcher = ReactCurrentDispatcher.current;
1818
invariant(
1919
dispatcher !== null,
2020
'Hooks can only be called inside the body of a function component.',

packages/react/src/ReactSharedInternals.js

+2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ import {
2929
unstable_unsubscribe,
3030
unstable_wrap,
3131
} from 'scheduler/tracing';
32+
import ReactCurrentDispatcher from './ReactCurrentDispatcher';
3233
import ReactCurrentOwner from './ReactCurrentOwner';
3334
import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';
3435

3536
const ReactSharedInternals = {
37+
ReactCurrentDispatcher,
3638
ReactCurrentOwner,
3739
// Used by renderers to avoid bundling object-assign twice in UMD bundles:
3840
assign,

0 commit comments

Comments
 (0)