Skip to content

Commit 0728d1f

Browse files
committed
feat: consider displayName of context types
1 parent 2c4921f commit 0728d1f

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

packages/react/src/ReactContext.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ export function createContext<T>(
120120
return context.Consumer;
121121
},
122122
},
123+
displayName: {
124+
get() {
125+
return context.displayName;
126+
},
127+
},
123128
});
124129
// $FlowFixMe: Flow complains about missing properties because it doesn't understand defineProperty
125130
context.Consumer = Consumer;

packages/react/src/__tests__/ReactContext-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('ReactContext', () => {
2121
ReactDOMServer = require('react-dom/server');
2222
});
2323

24-
it('ignores displayName on the context type', () => {
24+
it('should honor a displayName if set on the context type', () => {
2525
const Context = React.createContext(null);
2626
Context.displayName = 'MyContextType';
2727
function Validator() {
@@ -38,8 +38,8 @@ describe('ReactContext', () => {
3838
}).toErrorDev(
3939
'Warning: Failed prop type: The prop `dontPassToSeeErrorStack` is marked as required in `Validator`, but its value is `undefined`.\n' +
4040
' in Validator (at **)\n' +
41-
' in Context.Consumer (at **)\n' +
42-
' in Context.Provider (at **)',
41+
' in MyContextType.Consumer (at **)\n' +
42+
' in MyContextType.Provider (at **)',
4343
);
4444
});
4545
});

packages/shared/getComponentName.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
REACT_BLOCK_TYPE,
2525
} from 'shared/ReactSymbols';
2626
import {refineResolvedLazyComponent} from 'shared/ReactLazyComponent';
27+
import type {ReactContext, ReactProviderType} from 'shared/ReactTypes';
2728

2829
function getWrappedName(
2930
outerType: mixed,
@@ -37,6 +38,10 @@ function getWrappedName(
3738
);
3839
}
3940

41+
function getContextName(type: ReactContext<any>) {
42+
return type.displayName || 'Context';
43+
}
44+
4045
function getComponentName(type: mixed): string | null {
4146
if (type == null) {
4247
// Host root, text node or just invalid type.
@@ -73,9 +78,11 @@ function getComponentName(type: mixed): string | null {
7378
if (typeof type === 'object') {
7479
switch (type.$$typeof) {
7580
case REACT_CONTEXT_TYPE:
76-
return 'Context.Consumer';
81+
const context: ReactContext<any> = (type: any);
82+
return getContextName(context) + '.Consumer';
7783
case REACT_PROVIDER_TYPE:
78-
return 'Context.Provider';
84+
const provider: ReactProviderType<any> = (type: any);
85+
return getContextName(provider._context) + '.Provider';
7986
case REACT_FORWARD_REF_TYPE:
8087
return getWrappedName(type, type.render, 'ForwardRef');
8188
case REACT_MEMO_TYPE:

0 commit comments

Comments
 (0)