@samitouri / QOS-React-2 / commits / 7516bdfce3

feat(createContext): Include displayName in warning (#18386)

Sebastian Silbermann committed Apr 1, 2020 at 20:42 UTC 7516bdfce3f0f8c675494b5c5d0e7ae441bef1d9
2 files changed +7 -6
packages/react/src/ReactContext.js
+3 -2
@@ -125,11 +125,12 @@ export function createContext<T>(
125 get() {
126 return context.displayName;
127 },
128 - set() {
128 + set(displayName) {
129 if (!hasWarnedAboutDisplayNameOnConsumer) {
130 console.warn(
131 'Setting `displayName` on Context.Consumer has no effect. ' +
132 - "You should set it directly on the context with Context.displayName = 'NamedContext'.",
132 + "You should set it directly on the context with Context.displayName = '%s'.",
133 + displayName,
134 );
135 hasWarnedAboutDisplayNameOnConsumer = true;
136 }
packages/react/src/__tests__/ReactContextValidator-test.js
+4 -4
@@ -700,14 +700,14 @@ describe('ReactContextValidator', () => {
700 const Context = React.createContext(null);
701
702 expect(() => {
703 - Context.Consumer.displayName = 'ignored';
703 + Context.Consumer.displayName = 'IgnoredName';
704 }).toWarnDev(
705 'Warning: Setting `displayName` on Context.Consumer has no effect. ' +
706 - "You should set it directly on the context with Context.displayName = 'NamedContext'.",
706 + "You should set it directly on the context with Context.displayName = 'IgnoredName'.",
707 {withoutStack: true},
708 );
709
710 - // warning is deduped so subsequent setting is fine
711 - Context.Consumer.displayName = 'ignored';
710 + // warning is deduped by Context so subsequent setting is fine
711 + Context.Consumer.displayName = 'ADifferentName';
712 });
713 });