Skip to content

fix(core / styled): fix wrap the display name in styled and withEmotionCache - #1692

Closed
zthxxx wants to merge 3 commits into
emotion-js:masterfrom
zthxxx:master
Closed

fix(core / styled): fix wrap the display name in styled and withEmotionCache#1692
zthxxx wants to merge 3 commits into
emotion-js:masterfrom
zthxxx:master

Conversation

@zthxxx

@zthxxx zthxxx commented Dec 15, 2019

Copy link
Copy Markdown

What: display correct styled component's name in React DevTools

Why:

@emotion/styled use withEmotionCache function which is a HOC but not wrap a displayName.

So in React DevTools, it will be always show only render string as component name.

Before wrap displayName:

image

How:

Follow React displayName docs, inject displayName before and into withEmotionCache.

After wrap displayName:

image

Checklist:

  • Documentation N/A
  • Tests N/A
  • Code complete
  • Changeset

@changeset-bot

changeset-bot Bot commented Dec 15, 2019

Copy link
Copy Markdown

🦋 Changeset is good to go

Latest commit: eea168b

We got this.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codecov

codecov Bot commented Dec 15, 2019

Copy link
Copy Markdown

Codecov Report

Merging #1692 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted Files Coverage Δ
packages/styled-base/src/index.js 100% <100%> (ø) ⬆️
packages/core/src/context.js 100% <100%> (ø) ⬆️

@zthxxx

zthxxx commented Dec 15, 2019

Copy link
Copy Markdown
Author

cc @Andarist

Comment thread .changeset/nervous-otters-sell.md Outdated
})`

const injectDisplayName = Component => {
Component.displayName = Component.displayName || displayName

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no way for the Component to have a .displayName as it's always the anonymous function

// $FlowFixMe: we need to cast StatelessFunctionalComponent to our PrivateStyledComponent class
const Styled: PrivateStyledComponent<P> = withEmotionCache(
(props, context, ref) => {
const Styled: PrivateStyledComponent<P> = withEmotionCache<P>(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of doing this injectDisplayName I think it could be more readable if we could just split this into separate statements, like this:

const render = (props, context, ref) => {}
render.displayName = displayName
const Styled = withEmotionCache(render)

That being said - shouldn't we also make this displayName in here a little bit more distinct? Right now 2 component will receive the very same displayName from what I can see (the one being this render here and the other one being Styled)

@zthxxx zthxxx Dec 15, 2019

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now 2 component will receive the very same displayName from what I can see

In my practice, I found that Styled.displayName has no effect.

Origin Styled.displayName got the result as the screenshots shown above.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But first, i wrote codes like yours comment, but i found some problems that

  • its less semantic than injectDisplayNameToComponent(Component, displayName) func

  • if you want use the same code to solve same problems with other codes where withEmotionCache used, it will make lots of code changed;

    On the other hand injectDisplayName func is reusable logic if export and necessary

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that Styled.displayName has no effect.

i guess, its due to forwardRef in HOC

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess, its due to forwardRef in HOC

yes, source code at forwardRef.js#L46

seems do not accept Component.displayName after forwardRef return;

some POC codes and results here:

屏幕快照 2019-12-15 下午8 24 30

屏幕快照 2019-12-15 下午8 25 08

屏幕快照 2019-12-15 下午8 29 17

)
}

render.displayName = getDisplayName<Props>(func)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does func have a name under any circumstances? I think we always pass anonymous functions to it

@zthxxx zthxxx Dec 15, 2019

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not before, so I made injectDisplayName in styled-base to inject displayName before withEmotionCache,
the func still has no name property yet, but maybe post in the future in withEmotionCache, then withEmotionCache also ability to use name.

@zthxxx

zthxxx commented Dec 15, 2019

Copy link
Copy Markdown
Author

I try to fix it in react devtools in react/react#17613

@Andarist

Copy link
Copy Markdown
Member

Hm, this seems like some sort of regression in devtools. I've found a PR supposedly fixing this: https://github.com/facebook/react-devtools/pull/1154/files

@zthxxx

zthxxx commented Dec 16, 2019

Copy link
Copy Markdown
Author

yep, I think it's actually a problem in React DevTools.

@zthxxx zthxxx closed this Dec 16, 2019
@zthxxx

zthxxx commented Dec 18, 2019

Copy link
Copy Markdown
Author

Thanks for review, I have fixed the real reason in react/react#17613

@Andarist

Copy link
Copy Markdown
Member

No problem, I'm glad that you have fixed this in devtools ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants