-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Show displayName for Context Provider and Consumer #1031
Conversation
d4c865f to
73d8168
Compare
|
I'd love for this to be merged - @nanovazquez are you able to solve the merge conflict? ETA: just so we're clear I have no say in whether this gets merged 😄 |
0c39351 to
efb5ba5
Compare
|
@benwiley4000 done! 😄 |
|
I have not thought about this much, but my initial thought is that this would be a preferable API: const ThemeContext = React.createContext();
ThemeContext.displayName = 'ThemeContext';Then we could do something like: case CONTEXT_PROVIDER_NUMBER:
case CONTEXT_PROVIDER_SYMBOL_STRING:
nodeType = 'Special';
props = fiber.memoizedProps;
name = `${fiber.type._context.displayName || 'Context'}.Provider`;
children = [];
break;
case CONTEXT_CONSUMER_NUMBER:
case CONTEXT_CONSUMER_SYMBOL_STRING:
nodeType = 'Special';
props = fiber.memoizedProps;
name = `${fiber.type.displayName || 'Context'}.Consumer`;
children = [];
break;I'm also not sure about the coloring change. Seems unrelated to the core of the PR. FWIW you can probably avoid most of the merge conflicts that require rebasing by not committing files from |
|
@bvaughn I agree it would be nice to be able to not repeat yourself, but I think the least surprising thing would be to at least support putting the displayName directly on the consumer and the provider (which works for every other type of react component), and then maybe what you suggested. |
I don't think this is really true? It works for user-defined function and class components– and the user-supplied render function passed to |
|
@bvaughn ok sorry I did misspeak. I was really referring to user-defined components. The thing is that even though context components are created via a special factory, you (meaning I :) ) expect to be able to name them directly anyway because they feel like components I created. Unlike StrictMode or the like, Context.Consumer is specially instanced for my own purposes. |
|
Yeah, I can understand that point of view. I think my preference is still the same though. 😄 Another tiny reason why naming the context object feels preferable is that it avoids potentially mismatched names. |
|
Yeah, that's a good point |
|
I'm going to plug in my own change and do a little testing. If it seems good, I'll merge. |
|
Thanks! I've merged this. As of the next release, this should be supported: const ThemeContext = React.createContext();
ThemeContext.displayName = 'ThemeContext'; |
|
Thanks!! |
|
Thanks for your patience 😄 |
|
Released with 3.4.0 |
|
It doesn't feel intuitive that you can't set the display name separately on provider and consumer components; they are different components after all. A lot of libraries export a provider, called things like Here is an intuitive API: // example-lib.js
import React from 'react'
const {
Provider: ExampleProvider,
Consumer: ExampleConsumer
} = React.createContext()
ExampleProvider.displayName = 'ExampleProvider'
ExampleConsumer.displayName = 'ExampleConsumer'
export { ExampleProvider, ExampleConsumer }import React from 'react'
import { ExampleProvider } from 'example-lib'
const example = ''
const App = () => (
<ExampleProvider value={example}>
{/* Stuff */}
</ExampleProvider>
)It should display exactly as If I were to rethink the context API to make this easier, perhaps it could be: import React from 'react'
const {
ExampleProvider,
ExampleConsumer
} = React.createContext(
// Custom provider component name:
'ExampleProvider',
// Custom consumer component name:
'ExampleConsumer'
) |
|
I don't personally see why "ExampleProvider" and "ExampleConsumer" are inherently better than "Example.Provider" and "Example.Consumer". If anything, the latter provides a bit more of a visual cue that the two components are closely related as part of a single thing (the "Example" context). |
|
Agree with @bvaughn's reasoning, especially because |
|
@jaydenseric why would you want to give a different name to the provider and the consumer since they rely in the same context? |
|
From this PR, I'm still unsure how does it work if we set I would expect that I supposed this is roughly the same as this comment says about. |
This PR updates the code to use the displayName for the new React 16.3 Context API components. It includes the following changes:
TraceUpdatesBackendManagerto highlight updates in Context Consumer nodes. For this, it checks the type instead of the name.React.createContext, using one of ThemeContext examples in React's documentation.Before
React Devtools wasn't picking up the
displayNameor colorizing these nodes.After
If there is no display name in the Context components, React Devtools falls back to default values (
<Context.Provider>and<Context.Consumer>respectively).