Skip to content

Commit

Permalink
feat(createContext): Include displayName in warning (#18386)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Apr 1, 2020
1 parent 9b88b78 commit 7516bdf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/react/src/ReactContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ export function createContext<T>(
get() {
return context.displayName;
},
set() {
set(displayName) {
if (!hasWarnedAboutDisplayNameOnConsumer) {
console.warn(
'Setting `displayName` on Context.Consumer has no effect. ' +
"You should set it directly on the context with Context.displayName = 'NamedContext'.",
"You should set it directly on the context with Context.displayName = '%s'.",
displayName,
);
hasWarnedAboutDisplayNameOnConsumer = true;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/__tests__/ReactContextValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,14 +700,14 @@ describe('ReactContextValidator', () => {
const Context = React.createContext(null);

expect(() => {
Context.Consumer.displayName = 'ignored';
Context.Consumer.displayName = 'IgnoredName';
}).toWarnDev(
'Warning: Setting `displayName` on Context.Consumer has no effect. ' +
"You should set it directly on the context with Context.displayName = 'NamedContext'.",
"You should set it directly on the context with Context.displayName = 'IgnoredName'.",
{withoutStack: true},
);

// warning is deduped so subsequent setting is fine
Context.Consumer.displayName = 'ignored';
// warning is deduped by Context so subsequent setting is fine
Context.Consumer.displayName = 'ADifferentName';
});
});

0 comments on commit 7516bdf

Please sign in to comment.