Skip to content

Commit 989bb51

Browse files
committed
Make host context use null as empty and only error in dev
Makes it slightly more blazing.
1 parent 5f7ef8c commit 989bb51

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed

packages/react-reconciler/src/ReactFiberHostContext.new.js

+15-22
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,26 @@ import type {Container, HostContext} from './ReactFiberHostConfig';
1414
import {getChildHostContext, getRootHostContext} from './ReactFiberHostConfig';
1515
import {createCursor, push, pop} from './ReactFiberStack.new';
1616

17-
declare class NoContextT {}
18-
const NO_CONTEXT: NoContextT = ({}: any);
19-
20-
const contextStackCursor: StackCursor<HostContext | NoContextT> = createCursor(
21-
NO_CONTEXT,
22-
);
23-
const contextFiberStackCursor: StackCursor<Fiber | NoContextT> = createCursor(
24-
NO_CONTEXT,
17+
const contextStackCursor: StackCursor<HostContext | null> = createCursor(null);
18+
const contextFiberStackCursor: StackCursor<Fiber | null> = createCursor(null);
19+
const rootInstanceStackCursor: StackCursor<Container | null> = createCursor(
20+
null,
2521
);
26-
const rootInstanceStackCursor: StackCursor<
27-
Container | NoContextT,
28-
> = createCursor(NO_CONTEXT);
29-
30-
function requiredContext<Value>(c: Value | NoContextT): Value {
31-
if (c === NO_CONTEXT) {
32-
throw new Error(
33-
'Expected host context to exist. This error is likely caused by a bug ' +
34-
'in React. Please file an issue.',
35-
);
36-
}
3722

23+
function requiredContext<Value>(c: Value | null): Value {
24+
if (__DEV__) {
25+
if (c === null) {
26+
console.error(
27+
'Expected host context to exist. This error is likely caused by a bug ' +
28+
'in React. Please file an issue.',
29+
);
30+
}
31+
}
3832
return (c: any);
3933
}
4034

4135
function getCurrentRootHostContainer(): null | Container {
42-
const container = rootInstanceStackCursor.current;
43-
return container === NO_CONTEXT ? null : ((container: any): Container);
36+
return rootInstanceStackCursor.current;
4437
}
4538

4639
function getRootHostContainer(): Container {
@@ -61,7 +54,7 @@ function pushHostContainer(fiber: Fiber, nextRootInstance: Container) {
6154
// we'd have a different number of entries on the stack depending on
6255
// whether getRootHostContext() throws somewhere in renderer code or not.
6356
// So we push an empty value first. This lets us safely unwind on errors.
64-
push(contextStackCursor, NO_CONTEXT, fiber);
57+
push(contextStackCursor, null, fiber);
6558
const nextRootContext = getRootHostContext(nextRootInstance);
6659
// Now that we know this function doesn't throw, replace it.
6760
pop(contextStackCursor, fiber);

packages/react-reconciler/src/ReactFiberHostContext.old.js

+15-22
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,26 @@ import type {Container, HostContext} from './ReactFiberHostConfig';
1414
import {getChildHostContext, getRootHostContext} from './ReactFiberHostConfig';
1515
import {createCursor, push, pop} from './ReactFiberStack.old';
1616

17-
declare class NoContextT {}
18-
const NO_CONTEXT: NoContextT = ({}: any);
19-
20-
const contextStackCursor: StackCursor<HostContext | NoContextT> = createCursor(
21-
NO_CONTEXT,
22-
);
23-
const contextFiberStackCursor: StackCursor<Fiber | NoContextT> = createCursor(
24-
NO_CONTEXT,
17+
const contextStackCursor: StackCursor<HostContext | null> = createCursor(null);
18+
const contextFiberStackCursor: StackCursor<Fiber | null> = createCursor(null);
19+
const rootInstanceStackCursor: StackCursor<Container | null> = createCursor(
20+
null,
2521
);
26-
const rootInstanceStackCursor: StackCursor<
27-
Container | NoContextT,
28-
> = createCursor(NO_CONTEXT);
29-
30-
function requiredContext<Value>(c: Value | NoContextT): Value {
31-
if (c === NO_CONTEXT) {
32-
throw new Error(
33-
'Expected host context to exist. This error is likely caused by a bug ' +
34-
'in React. Please file an issue.',
35-
);
36-
}
3722

23+
function requiredContext<Value>(c: Value | null): Value {
24+
if (__DEV__) {
25+
if (c === null) {
26+
console.error(
27+
'Expected host context to exist. This error is likely caused by a bug ' +
28+
'in React. Please file an issue.',
29+
);
30+
}
31+
}
3832
return (c: any);
3933
}
4034

4135
function getCurrentRootHostContainer(): null | Container {
42-
const container = rootInstanceStackCursor.current;
43-
return container === NO_CONTEXT ? null : ((container: any): Container);
36+
return rootInstanceStackCursor.current;
4437
}
4538

4639
function getRootHostContainer(): Container {
@@ -61,7 +54,7 @@ function pushHostContainer(fiber: Fiber, nextRootInstance: Container) {
6154
// we'd have a different number of entries on the stack depending on
6255
// whether getRootHostContext() throws somewhere in renderer code or not.
6356
// So we push an empty value first. This lets us safely unwind on errors.
64-
push(contextStackCursor, NO_CONTEXT, fiber);
57+
push(contextStackCursor, null, fiber);
6558
const nextRootContext = getRootHostContext(nextRootInstance);
6659
// Now that we know this function doesn't throw, replace it.
6760
pop(contextStackCursor, fiber);

0 commit comments

Comments
 (0)