reactjs - reactjs 切换自定义主题无法按预期工作
问题描述
我有三个基本的自定义主题,我想在它们之间切换。
我有一些问题/疑问:
- 为什么来自 CustomToolbar.jsx 的容器正在“工作”,而来自 Component1.jsx 的容器却没有?
- 为什么 App.js 中的按钮正在改变背景而组件中的按钮没有?(也应该)
- 有时,当我多次单击 CustomToolbar 中的按钮时,出现错误:
Warning: Failed prop type: The prop `theme` is marked as required in `ThemeProvider`, but its value is `undefined`.
in ThemeProvider (at CustomThemeProvider.js:37)
in CustomThemeProvider (at src/index.js:11)
我不知道如何管理它们,任何如何改进代码的帮助都会很棒。
解决方案
问题 1 和 2:这是因为您将每个组件都包装在ThemeProvider
/MuiThemeProvider
中,这样做会覆盖您的自定义主题。
App
将你的内部包裹CustomThemeProvider
在你的 index.js 上就足够了。
问题3:单击时e.target.innerHTML
不会总是返回按钮内的文本。尝试控制台记录,然后单击按钮而不是文本,您会看到类似
<span class="MuiButton-label">red</span><span class="MuiTouchRipple-root"><span class="MuiTouchRipple-ripple MuiTouchRipple-rippleVisible" style="width: 128.825px; height: 128.825px; top: -35.4127px; left: -8.41273px;"><span class="MuiTouchRipple-child MuiTouchRipple-childLeaving"></span></span></span>
要解决此问题,您可以e.currentTarget.textContent
改用。
请查看下面的演示。
推荐阅读
- python - 如何从文件夹中选择带有编号扩展名的文件?
- swift - 如何跨场景传递价值观
- javascript - 如何使用键打开并专注于 JQuery 中已清除的输入元素?
- c# - 在 JSON.NET 中序列化集合不起作用
- python - 如何在 Python 中格式化和打印在同一行
- json - Datatables.net - 无效的 Json 响应
- java - 如何在Android中特定时间后显示通知
- php - Laravel 在控制器中嵌套子视图的问题
- gradle - 您如何定义变量以排除 build.gradle.kts 中的多个传递依赖项
- mongodb - MongoDB,正确设置 OneToMany、ManyToOne 关系方法