-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicated MuiButtonBase (and others) in <style> elements #15610
Comments
@marc-polizzi How many theme providers do you use? |
@oliviertassinari the application is using several
statements but in the faulty usecases we're using a single one. I've put a breakpoint in the Chrome browser and only the one of our top level component seems called. Something like:
|
@marc-polizzi Two things. If you are using multiple ThemeProvider, you need to provide one at the root. You can change the class name generation to output non-global class names with the |
@marc-polizzi Do you use multiple React mount point? |
If you mean we're calling several times
Still have the same issue. |
@marc-polizzi Then I'm out of ideas. You need to run a dichotomy on your React Tree. |
@oliviertassinari sorry not sure to understand; what do you mean by "run a dichotomy" ? |
You can use a dichotomy to find the source of the problem. Unmount half your tree, see if the problem is still present, repeat. |
@marc-polizzi Let us know when you find the source of the problem. We would be happy to understand how we can improve the implementation. I'm closing, waiting for a reproduction. Thanks for the report. |
Here is a little example - self contained in the index.tsx file - that is failing in my project. Unfortunately, I cannot reproduce the same behavior in codesandbox.io. When running the following React app, I can see several MuiButtonBase CSS rules generated creating some unexpected conflicts. It seems this is related to the import:
Importing as following is solving the issue:
I'm still investigating... Any advice / feedback?
|
@marc-polizzi You should get a style module duplication warning in the console in this case with v4.0.0-beta.1. You might bundle two versions of the components: the CJS and ESM. |
I fear we can't help more without a reproduction. |
I've copy / pasted that file index.tsx into a codesandbox.io but got no issue. I guess something is wrong with our project configuration but have no idea where to look at. In the meantime, we've added a prebuild script that is grepping our files for this kind of imports. |
@marc-polizzi Be sure that your path imports are not too deep. import { Button } from '@material-ui/core'; // Ok
import Button from '@material-ui/core/Button'; // Ok
import Button from '@material-ui/core/Button/Button'; // Not Ok If you're building with wepack you could use something like webpack-bundle-analyzer or put the created stats.json into webpack-analyse to see which modules are importing creating the duplicate module. |
I have tried to reproduce the style duplication warning without luck: https://codesandbox.io/s/affectionate-ramanujan-mnjwg. |
I'm having this same issue. .MuiButtonBase-root is being applied twice and is overriding any other styling. I have checked the "classes" and "theme" props and those do not appear to change between different account detail views nor after the component mounts. The MUI code:
These are the exact same component. Is there anything aside from the "classes" and "theme" props that might cause this? Also, In our components that use MUI's ExpansionPanel component, the .MuiButtonBase-root class is applied again and overrides the MUI's own .MuiExpansionPanelSummary-root styling.
|
One of our dependencies also had material-ui as a dependency. We changed material-ui to a peer dependency and our issues are now fixed! |
Just cross-posting mui/material-ui-pickers#1143 (comment) here, in case it helps anyone. I got duplicate style tags, which caused some styles to be wrong (in my case, styles from the I think it might be possible to use the Original commentI've experienced the same issue, tracked the cause to this: #15610. I followed #15610 (comment), and fixed all imports that was double nested, eg. changed import Button from '@material-ui/core/Button/Button'; to import Button from '@material-ui/core/Button'; I had a LOT of imports to fix (did a search on You can check if you have the same problem with duplicate imports, by going in to your Chrome DevTools > Sources > expand |
@JReinhold Thanks for sharing your experience. We are using a custom eslint rule on the repository: https://github.com/mui-org/material-ui/blob/master/packages/eslint-plugin-material-ui/src/rules/restricted-path-imports.js for our own use cases. I would personally encourage people to import from the barrel index: import { Button } from '@material-ui/core'; |
Oh wow, that's cool, is it possible that it can get published? I can see that it is on the registry, however that published version is 3 years old and doesn't contain that rule.
Yeah, we've restrained from doing that specifically, because in our experience, it slows down the TypeScript compiler a considerable amount. However I've just tested with a few components, and that doesn't seem to be the case anymore (maybe something has changed in v4?), so that's probably a good option. |
@JReinhold The eslint rule was written by @eps1lon. I have no objection to publishing it. If the other maintainers agree, we can go for it.
I'm happy to hear that the performance overhead is no longer present! With #16192, you might be able to automate the migration, at least, to try it. |
|
The issue also exists in my development environment when I use We have two projects A and B I get used to link B to A for debugging. When I run the dev-server on project A, B will look for the module from the nearest path which is installed in directory of B:
Therefore, the
Hope for any better solution, thank you. |
@brandonccx #15610 (comment) is a great solution. |
Hi guys, It turned out that I had to change the way to build my react/material-Ui components library 1- Make sure that in the library, all imports where such as 2- Remove the usage of 3- Push 4- Rebuilt the library using webpack and babel to compile typescript tsx to js. Then, importing the library in the main APP, creating the theme and applying it at the root of the App worked like a charm, whereas I had stronge behavior from one component to another before applying this method. However to be fair, I do not really understand the reason why this refactoring and the move from dependency to dev and peer fixed the problem. |
* Fix an issue which caused multiple styles to be applied in the incorrect order. For details about the duplicated style issue please see this issue in material-ui: mui/material-ui#15610 * Add a lint check to ensure the issue never returns. * Fix linter execution so it should pass on CI.
* Fix an issue which caused multiple styles to be applied in the incorrect order. For details about the duplicated style issue please see this issue in material-ui: mui/material-ui#15610 * Add a lint check to ensure the issue never returns. * Fix linter execution so it should pass on CI.
It's closed, but just my two cents: resolve: {
alias: {
'@material-ui/core': path.resolve(__dirname, 'node_modules', '@material-ui/core')
}
} |
We're running into the same issue, but in our case with components that come from 'lab': Neither of the following seem to work:
There's always duplicate MuiBaseButton style definitions. Any thoughts? 🙏 @oliviertassinari maybe? EDIT: w/ the following versioning btw ├─ @material-ui/core@4.11.0 |
I also have a duplicate I'm pretty sure the problem comes from two bundles that are included on the same page as mentioned here. Since the
Then I included the This did not fix the issue. Is there anything I need to change with this config? As far as I can tell (via the Chrome DevTools > Sources page and by looking through the code) I don't have any wrong imports. |
My use case is similar to @jeromeSH26 - creating a library of common components to be used in multiple projects within an organization. His advice is good, but in order to get this fully working for my setup, I also had to declare all material UI components as externals in the webpack config for my library, and I did this with a regex like so:
Everything is now working as expected. Hope this helps someone. |
@nkpz does using webpack externals require including a script tag (in html where bundles and entry point is )with src being a CDN url? |
@avelosa No, defining an external will just tell it not to bundle the dependency and to look for it elsewhere in the bundle at runtime. It's useful for authoring a component library as an addon to mui because everything using your library will already have its own version of react, react-dom, material-ui etc |
I'm having this issue when using mui components with aggrid. I'm not sure if it's mui or aggrid causing the issue, but I've made a min repo showing the issue https://github.com/areisle/aggrid-mui-styles-issue and deployed it here https://aggrid-mui-issue.herokuapp.com/ |
It's getting weirder by the |
I had same issue resolve: {
alias: {
'@material-ui/styles': path.resolve('./node_modules/@material-ui/styles'),
'@material-ui/core': path.resolve('./node_modules/@material-ui/core'),
}
} |
I had to change the webpack config to the following to get it to work for our component library:
When I used the regex rule, it was complaining about an invalid |
I am glad it worked for you. Please give an update if you later find a concrete difference between 'commonjs module' and 'commonjs2'. |
Any solution for CRA? (we don't have webpack config) |
See #15610 (comment); I'm using CRA. |
This doesn't help. I already have my "strictMode" removed long ago. I already tried moving both these packages into dev & peer dependencies. |
The docs: https://material-ui.com/getting-started/faq/#i-have-several-instances-of-styles-on-the-page, this behavior doesn't impact v5. |
The
<head>
section contains several times the same material-ui internal<style>
creating conflicts in styles being applied. The content of those<style>
are not using unique names as before it seems: .jss346...Expected Behavior 🤔
For example, .MuiButtonBase-root should be present once only.
Current Behavior 😯
.MuiButtonBase-root is added 2/3 times.
Steps to Reproduce 🕹
Cannot reproduce with a simple piece of code; even in our application sometimes it's ok sometimes not. Tell us how to troubleshoot on our side to provide more information.
Context 🔦
Nothing special I believe. Components are badly rendered because of material-ui internal styles are being applied out of order.
Your Environment 🌎
This issue appeared since v4 beta.0 (still in beta.1): v4 alpha were ok.
The text was updated successfully, but these errors were encountered: