Skip to content
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

MUI + NextJS + Redux - Dark Theme switch not working on SSR #44949

Closed
karthikdivi opened this issue Jan 6, 2025 · 4 comments
Closed

MUI + NextJS + Redux - Dark Theme switch not working on SSR #44949

karthikdivi opened this issue Jan 6, 2025 · 4 comments
Assignees
Labels
nextjs support: question Community support but can be turned into an improvement

Comments

@karthikdivi
Copy link

karthikdivi commented Jan 6, 2025

Steps to reproduce

Steps:

  1. Open this link to live example: (required)
  2. Repo: https://github.com/karthikdivi/thefinalstack/tree/main
  3. Run the following commands to start npm install --legacy-peer-deps and npm run dev

Current behavior

Dark mode not getting applied.

Expected behavior

On refreshing the page the dark mode should apply.

Context

When I switch the theme to Dark mode, it updates the Redux state and also preserved to LocalStorage. When I do a hard refresh in the browser, reading the theme preference from Redux and applying it. But the Dark theme is not showing.

Screen.Recording.2025-01-06.at.1.53.20.PM.mov

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
    System:
    OS: macOS 14.5
  Binaries:
    Node: 18.20.5 - ~/.nvm/versions/node/v18.20.5/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v18.20.5/bin/npm
    pnpm: Not Found
  Browsers:
    Chrome: 131.0.6778.205
    Edge: Not Found
    Safari: 17.5
  npmPackages:
    @emotion/react: latest => 11.14.0 
    @emotion/styled: latest => 11.14.0 
    @mui/core-downloads-tracker:  6.3.1 
    @mui/icons-material: latest => 6.3.1 
    @mui/material: latest => 6.3.1 
    @mui/material-nextjs: latest => 6.3.1 
    @mui/private-theming:  6.3.1 
    @mui/styled-engine:  6.3.1 
    @mui/system:  6.3.1 
    @mui/types:  7.2.21 
    @mui/utils:  6.3.1 
    @types/react:  19.0.2 
    react: ^19.0.0 => 19.0.0 
    react-dom: ^19.0.0 => 19.0.0 
    typescript:  5.7.2 

Search keywords: nextjs dark mode

@karthikdivi karthikdivi added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 6, 2025
@zannager zannager added the nextjs label Jan 6, 2025
@siriwatknp
Copy link
Member

Is it required to use Redux? If not, I recommend following:

@siriwatknp siriwatknp added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 6, 2025
@karthikdivi
Copy link
Author

Is it required to use Redux? If not, I recommend following:

@siriwatknp I have updated the code with InitColorSchemeScript and it's already using the cssVariables. Still the issue persists.
Also do you have any reference implementation project with Dark mode toggle?

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Jan 6, 2025
@DiegoAndai DiegoAndai moved this to Selected in Material UI Jan 9, 2025
@DiegoAndai DiegoAndai moved this from Selected to Backlog in Material UI Jan 9, 2025
@siriwatknp
Copy link
Member

@karthikdivi Please check my commit. The fix is to combine light and dark theme into a single theme within colorSchemes and let Material UI manage the mode for you.

I don't see a need to use redux. If you want to get the selected mode, you can use useColorScheme hook from Material UI.

@github-project-automation github-project-automation bot moved this from Backlog to Done in Material UI Jan 10, 2025
@siriwatknp siriwatknp added support: question Community support but can be turned into an improvement and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 10, 2025
Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@karthikdivi How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nextjs support: question Community support but can be turned into an improvement
Projects
Status: Done
Development

No branches or pull requests

3 participants