Skip to content

[docs] Add Next.js App Router guide for custom classnames #45852

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

Merged
merged 3 commits into from
Apr 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,36 @@ Always create an initializer file to hoist the `ClassNameGenerator` call to the

```js
// create a new file called `MuiClassNameSetup.js` at the root or src folder.
'use client'; // remove this line if you are not using React Server Components
import { unstable_ClassNameGenerator as ClassNameGenerator } from '@mui/material/className';

ClassNameGenerator.configure(
// Do something with the componentName
(componentName) => componentName,
);

export default null;
```

Then import the file in the main JavaScript source based on the framework.

### Next.js App Router

Add the `'use client'` directive and import the initializer in `/app/page.js`.

```diff
+'use client';
+import './MuiClassNameSetup';
import * as React from 'react';
import Button from '@mui/material/Button';

export default function Page() {
return (
<Button>Text</Button>
);
}
```

### Next.js Pages Router

Import the initializer in `/pages/_app.js`.
Expand Down
Loading