Skip to content

Commit

Permalink
dedupe warning
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Mar 5, 2020
1 parent ffabb83 commit 2176e6d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/react/src/ReactContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function createContext<T>(

let hasWarnedAboutUsingNestedContextConsumers = false;
let hasWarnedAboutUsingConsumerProvider = false;
let hasWarnedAboutDisplayNameOnConsumer = false;

if (__DEV__) {
// A separate object, but proxies back to the original context object for
Expand Down Expand Up @@ -125,10 +126,13 @@ export function createContext<T>(
return context.displayName;
},
set() {
console.warn(
'Setting `displayName` on Context.Consumer has no effect. ' +
"You should set it directly on the context with Context.displayName = 'NamedContext'.",
);
if (!hasWarnedAboutDisplayNameOnConsumer) {
console.warn(
'Setting `displayName` on Context.Consumer has no effect. ' +
"You should set it directly on the context with Context.displayName = 'NamedContext'.",
);
hasWarnedAboutDisplayNameOnConsumer = true;
}
},
},
});
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/__tests__/ReactContextValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,5 +706,8 @@ describe('ReactContextValidator', () => {
"You should set it directly on the context with Context.displayName = 'NamedContext'.",
{withoutStack: true},
);

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

0 comments on commit 2176e6d

Please sign in to comment.