Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: honor displayName of context types #18035

Merged
merged 3 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/react/src/ReactContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export function createContext<T>(
return context.Consumer;
},
},
displayName: {
get() {
return context.displayName;
},
},
});
// $FlowFixMe: Flow complains about missing properties because it doesn't understand defineProperty
context.Consumer = Consumer;
Expand Down
45 changes: 45 additions & 0 deletions packages/react/src/__tests__/ReactContext-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

let PropTypes;
let React;
let ReactDOMServer;

describe('ReactContext', () => {
beforeEach(() => {
jest.resetModules();
PropTypes = require('prop-types');
React = require('react');
ReactDOMServer = require('react-dom/server');
});

it('should honor a displayName if set on the context type', () => {
const Context = React.createContext(null);
Context.displayName = 'MyContextType';
function Validator() {
return null;
}
Validator.propTypes = {dontPassToSeeErrorStack: PropTypes.bool.isRequired};

expect(() => {
ReactDOMServer.renderToStaticMarkup(
<Context.Provider>
<Context.Consumer>{() => <Validator />}</Context.Consumer>
</Context.Provider>,
);
}).toErrorDev(
'Warning: Failed prop type: The prop `dontPassToSeeErrorStack` is marked as required in `Validator`, but its value is `undefined`.\n' +
' in Validator (at **)\n' +
' in MyContextType.Consumer (at **)\n' +
' in MyContextType.Provider (at **)',
);
});
});
eps1lon marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 9 additions & 2 deletions packages/shared/getComponentName.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
REACT_BLOCK_TYPE,
} from 'shared/ReactSymbols';
import {refineResolvedLazyComponent} from 'shared/ReactLazyComponent';
import type {ReactContext, ReactProviderType} from 'shared/ReactTypes';

function getWrappedName(
outerType: mixed,
Expand All @@ -37,6 +38,10 @@ function getWrappedName(
);
}

function getContextName(type: ReactContext<any>) {
return type.displayName || 'Context';
}

function getComponentName(type: mixed): string | null {
if (type == null) {
// Host root, text node or just invalid type.
Expand Down Expand Up @@ -73,9 +78,11 @@ function getComponentName(type: mixed): string | null {
if (typeof type === 'object') {
switch (type.$$typeof) {
case REACT_CONTEXT_TYPE:
return 'Context.Consumer';
const context: ReactContext<any> = (type: any);
return getContextName(context) + '.Consumer';
case REACT_PROVIDER_TYPE:
return 'Context.Provider';
const provider: ReactProviderType<any> = (type: any);
return getContextName(provider._context) + '.Provider';
case REACT_FORWARD_REF_TYPE:
return getWrappedName(type, type.render, 'ForwardRef');
case REACT_MEMO_TYPE:
Expand Down