Skip to content

Commit 2176e6d

Browse files
committed
dedupe warning
1 parent ffabb83 commit 2176e6d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/react/src/ReactContext.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export function createContext<T>(
5757

5858
let hasWarnedAboutUsingNestedContextConsumers = false;
5959
let hasWarnedAboutUsingConsumerProvider = false;
60+
let hasWarnedAboutDisplayNameOnConsumer = false;
6061

6162
if (__DEV__) {
6263
// A separate object, but proxies back to the original context object for
@@ -125,10 +126,13 @@ export function createContext<T>(
125126
return context.displayName;
126127
},
127128
set() {
128-
console.warn(
129-
'Setting `displayName` on Context.Consumer has no effect. ' +
130-
"You should set it directly on the context with Context.displayName = 'NamedContext'.",
131-
);
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'.",
133+
);
134+
hasWarnedAboutDisplayNameOnConsumer = true;
135+
}
132136
},
133137
},
134138
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,5 +706,8 @@ describe('ReactContextValidator', () => {
706706
"You should set it directly on the context with Context.displayName = 'NamedContext'.",
707707
{withoutStack: true},
708708
);
709+
710+
// warning is deduped so subsequent setting is fine
711+
Context.Consumer.displayName = 'ignored';
709712
});
710713
});

0 commit comments

Comments
 (0)