Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
863 views
in Technique[技术] by (71.8m points)

reactjs - Material-UI unchanged colors when switching back and forth between themes

I'm changing the theme in my application via a state variable and the MuiThemeProvider When switching the active theme the colors on all components update correctly, but only once. If I switch back to a theme that's already been used the colors do not update.

https://codesandbox.io/s/focused-mcnulty-0tzm2?file=/src/App.js

You can see that on the codesandbox, switching back and forth causes the last theme to "stick" if the theme you're changing to has already been used.

I can "fix" this issue by using theme.palette.primary.main on makeStyles() when styling my components but that kinda defeats the purpose of the color properties for the components

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Material-UI does not yet fully support StrictMode (https://github.com/mui-org/material-ui/issues/20708). If you remove the <React.StrictMode> tags in index.js it works fine.

Here's a working version of your sandbox: https://codesandbox.io/s/strictmode-theme-switching-issues-7prfe.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...