简体   繁体   中英

Material-UI theme does not change back

I've been trying to apply theme to my React app with Material-UI ThemeProvider and am encountering some strange behavior.

I have two themes: day mode and night mode. At app start, night mode is default, and user can switch theme with checkbox.

Theme switching normally from night mode to day mode, but it does not switch back.

Here is the example app, reproducing this behavior:
https://codesandbox.io/s/eloquent-ives-k1np1

What am I doing wrong?

Okay after some changes it looks like you need a fresh copy of the theme to be set in the state each time. I know material memoizes the provider but not sure why your example does not work...

here is a codesandbox with a working version... the only change is making a shallow copy of the theme, you may want to just create the theme on demand instead of storing it locally.

https://codesandbox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM