问题描述
我正在通过状态变量和 MuiThemeProvider
更改应用程序中的主题切换活动主题时,所有组件上的颜色均会正确更新,但只能更新一次.如果我切换回已经使用的主题,则颜色不会更新.
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
您可以看到,在codesandbox上,来回切换会导致最后一个主题粘住",如果您要更改的主题已被使用.
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.
我可以修复"在对组件进行样式设置时,在 makeStyles()
上使用 theme.palette.primary.main
会出现此问题,但这有点违反了 color
属性的目的组件
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
推荐答案
材料UI尚未完全支持 StrictMode
( https://github.com/mui-org/material-ui/issues/20708 ).如果您删除index.js中的< React.StrictMode>
标记,则效果很好.
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.
这是您的沙盒的工作版本: https://codesandbox.io/s/strictmode-theme-switching-issues-7prfe .
Here's a working version of your sandbox: https://codesandbox.io/s/strictmode-theme-switching-issues-7prfe.
这篇关于在主题之间来回切换时,Material-UI不变的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!