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.

[withStyles] Class names are regenerated every time the theme changes.

See original GitHub issue

If I have a HOC: withStyles({ root: { fontWeight: 'bold' } }) it will get turned into a class name, such as .MyComponent-root-1. If I now change my theme on the MuiThemeProvider component, this HOC re-generates this class name as .MyComponent-root-2. In the majority of cases, this effect is unnoticed, but I am an edge case where I am changing the theme semi-rapidly. As a result, the memory used by my page maxes out as the class names count rapidly towards infinity.

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

Expected Behavior

Class names should not change if the class definition did not change.

Current Behavior

Class names change, no matter what, even if the object reference does not change.

Steps to Reproduce

Make any withStyles HOC and change the theme.

Context

I’d like to change my theme without the entire DOM being altered. This is a major performance problem.

At the very least, I believe unchanging object references should prevent re-rendering:

let cache = {};
let color = 'red';
const myStyles = (theme) => {

  // Only change object reference if object changes.
  if (theme.palette.primary.main!== color ) {
    color = theme.palette.primary.main;
    cache = {
      myClassName: { color }
    };
  }
  return cache;
};

This will not impact the majority of users who use:

const myStyles = (theme) => ({
  myClassName: {
    color: theme.palette.primary.main
  }
});

But it should give a performance boost to anyone who is conscious about such things (or needs to be, like myself).

I wouldn’t mind PR’ing this myself, but I do not know where to look for where these class names are generated. It doesn’t seem like much of a change. A shallow comparison should work fine. if (currentJSS === previousJSS) { return lastClassName; } else { return createNewClassName(); }.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
CharlesStovercommented, Mar 19, 2019

@thucngdg This issue is solved. You need to move <MuiThemeProvider> outside of the Form component. Make it a parent. <MuiThemeProvider theme={createMuiTheme(muiTheme)}><Form /></MuiThemeProvider> for example.

Best practice is to wrap your entire <App /> in <MuiThemeProvider>. You do not have to use <MuiThemeProvider> in each Component that uses a MUI Theme. You only use it once – like the Redux <Provider> Component that you have wrapping your entire <App />. Put the <MuiThemeProvider> in the same place. 😃

0reactions
thucngdgcommented, Mar 19, 2019

Thanks 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

[withStyles] Class names are regenerated every time ... - GitHub
[withStyles] Class names are regenerated every time the theme changes. #12437 · Expected Behavior. Class names should not change if the class ...
Read more >
Material-UI Class names keep changing every time I hit refresh
I just want to keep the one class name so that I can make actual changes to my input fields. reactjs · typescript...
Read more >
Advanced (LEGACY) - MUI System
The wrapped component accepts a classes prop, it simply merges the class names provided with the style sheet. const Nested = withStyles({ root:...
Read more >
@mui/private-classnames | Yarn - Package Manager
@mui/private-classnames. React classnames utils used internally in the other MUI component libraries packages. ... Here are some highlights ✨:.
Read more >
Styling - Remix
Styling. The primary way to style in Remix (and the web) is to add a <link rel="stylesheet"> to the page. In Remix, you...
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