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.

createGenerateClassName in Material UI v5

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

What’s the proper alternative to legacy https://mui.com/styles/api/#creategenerateclassname-options-class-name-generator if @mui/styles package is not used, and all styles are created via MUI System (sx)?

By default all classes (even on production) are prefixed with Mui and css which may become a problem if multiple MUI apps are on the same page (Module Federation especially):

image

Expected behavior 🤔

Documentation is only available for legacy implementation, I’ve scanned the whole site, including migration instructions, and found no docs related to how to do it properly.

Steps to reproduce 🕹

Steps:

  1. Open https://codesandbox.io/s/material-ui-issue-latest-s2dsx
  2. Check the devtools:
<p class="MuiTypography-root MuiTypography-body1 css-ahj2mt-MuiTypography-root">MUI example. Please put the code to reproduce the issue in src/Demo.js</p>

Context 🔦

I need to have 2 (or more) Material-UI based apps (potentially with different versions) on the same page w/o clashing styles.

Your environment 🌎

`npx @mui/envinfo`
@emotion/react latest (11.7.0)
@emotion/styled latest (11.6.0)
@mui/material latest (5.2.2)
react latest (17.0.2)
react-dom latest (17.0.2)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
siriwatknpcommented, Mar 7, 2022

@mnajdova I’ve slightly changed your demo, please take a look. When you’ll get to 5 there should be a generated CSS selector to hide the Badge (like this .css-fvc8ir-RCWBBadge-badge.RCWBBadge-invisible), but there isn’t (there is such selector though .css-fvc8ir-MuiBadge-badge.MuiBadge-invisible).

https://codesandbox.io/s/mui-class-generator-forked-kd9dw?file=/src/Demo.tsx

The issue is fixed, you have to change the import:

-import { unstable_ClassNameGenerator } from '@mui/material/utils';
+import { unstable_ClassNameGenerator } from '@mui/material/className';

https://mui.com/guides/classname-generator/#main-content

1reaction
kirill-konshincommented, Dec 7, 2021

I’ve also found out that there’s an alternative way:

import { CacheProvider } from '@emotion/react';

const stylesCache = createCache({
  key: 'rcwb',
});

// and then
<CacheProvider value={stylesCache}>...</CacheProvider>

Is this legit? Which one is better, this one or via ClassNameGenerator.configure?

Read more comments on GitHub >

github_iconTop Results From Across the Web

createGenerateClassName in Material UI v5 - Stack Overflow
Show activity on this post. Have a look at https://mui.com/guides/classname-generator/, it is the replacement of createGenerateClassName .
Read more >
createGenerateClassName in Material UI v5 #30011 - GitHub
I need to have 2 (or more) Material-UI based apps (potentially with different versions) on the same page w/o clashing styles. Your environment ......
Read more >
Styles API - MUI System
StrictMode or React 18. createGenerateClassName([options]) => class name generator. A function which returns a class name ...
Read more >
ClassName generator - Material UI - MUI
Configure classname generation at build time. This API is introduced in @mui/material (v5.0.5) as a replacement of deprecated createGenerateClassName .
Read more >
ClassName Generator - MUI
Configure classname generation at build time. This API is introduced in @mui/material (v5.0.5) as a replacement of deprecated createGenerateClassName .
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