question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[4.0.1-beta] Unwanted CSSStyleSheet.insertRule behavior when using styled (passing a function as a param for styles)

See original GitHub issue
  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

All styles should be internal in DOM, i.e. the actual CSS should be inlined in the <style>'s tags in the <head>. Why? Because Prerendering doesn’t work otherwise as the styles, instead of being internal, are directly inserted into the CSSStyleSheet, using insertRule (the process is so-called the speedy mode). More info:

Current Behavior 😯

Common Header for the following two examples:

import { styled } from "@material-ui/styles"
const component = (props) => <div {...props} /> // just an example
  • Works great if using without accessing props, resulting in internal css, rendered in DOM:
export const Div = styled(component)({
  width: `100%`,
})
  • Uses unwanted CSSStyleSheet.insertRule, not rendering CSS in DOM, directly applying styles to the CSSStyleSheet:
export const Div = styled(component)((props) => ({ // IMPORTANT! Note passing the function
  width: `100%`,
}))

Your Environment 🌎

  "dependencies": {
    "@material-ui/core": "4.0.0-beta.1",
    "@material-ui/styles": "4.0.0-beta.1",
    "react": "16.8.6",
    "react-dom": "16.8.6"
  }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
eps1loncommented, May 13, 2019

This is best discussed in an issue in the Jss repo. It seems like the decision was made consciously. Please reference the original issue there so that anybody searching for a solution here is properly forwarded.

It would be interesting to know how other styling solution solve this.

@o-alexandrov I would suggest you change your tone to be less prescriptive. At least for me it’s not obvious from this issue why the current approach was chosen. You saying that it should not be chosen sounds like you either know better and don’t want to share knowledge or you don’t know the full story and act like you do. Either way isn’t constructive at all.

3reactions
o-alexandrovcommented, May 13, 2019

@eps1lon

Please reference the original issue there so that anybody searching for a solution here is properly forwarded.

Please be informed that in this post above I actually:

  • created a separate issue
  • described everything in detail there also
  • mentioned that I solved the issue I was having with Prerendering and steps to follow to resolve for others
  1. I am surprised to see detailed explanations to be met with something other than gratitude.
  2. Regarding the tone, it is again surprising to see the answer in great detail expressing nuances to a question that could be easily Google’d.
    • If I asked someone a question that I could then find as a first link on Google, I would feel deeply sorry to wasting someone’s time, it is surprising to see a disagreement on this.
  3. Regarding the:

it’s not obvious from this issue why the current approach was chosen. You saying that it should not be chosen sounds like you either know better and don’t want to share knowledge or you don’t know the full story and act like you do

  • I have provided you in this discussion with:
    • code samples, such as this benchmark from the mentioned above issue
    • extremely detailed explanations above, including screencasts, and explanation of terminology

I am sad to see such misinterpretation of the discussion, and I am sincerely sorry for the tone that you seem to interpreted as prescriptive. On the other hand, I hope you consider to offer more understanding to those, like me, whose English is not the first language and thus would know, that the tone might simply be something you are not used to growing up in a different culture.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[4.0.1-beta] Unwanted CSSStyleSheet.insertRule behavior ...
[4.0.1-beta] Unwanted CSSStyleSheet.insertRule behavior when using styled (passing a function as a param for styles) #15660.
Read more >
CSSStyleSheet.insertRule() - Web APIs | MDN
The CSSStyleSheet.insertRule() method inserts a new CSS rule into the current style sheet.
Read more >
Passing props to MUI styles - reactjs - Stack Overflow
Solution for how to use both props and theme in material ui : ... Seriously, if you're styling a function component, use makeStyles...
Read more >
Advanced Usage - styled-components
This function will receive the parent theme, that is from another <ThemeProvider> higher up the tree. This way themes themselves can be made...
Read more >
Demystifying styled-components - Josh W Comeau
What about the styles parameter that we passed to the h1 function? How does it get used? When we render the Title component,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found