我正在尝试使用按钮或切换按钮(我稍后会决定)将我的反应应用程序的主题从浅色更改为深色(反之亦然)。事实是:我直接从 bootswatch 导入主题的 .css(我目前使用“Minty”作为浅色主题,我想使用“Darkly”作为深色主题(https://bootswatch.com/))。目前我只发现有 ThemeProvider 组件,但它需要主题作为 .js 文件,您必须在其中导出您想要的内容(我不能这样做,因为我只有 .css)。我还在 github 上找到了一个 ThemeSwitcher(https://github.com/raythird/react-bootstrap-theme-switcher),但它似乎使用的是 bootstrap v3,而我实际上需要 v4。Reddit 上还有一个帖子建议将它们导入公共文件夹并更改它们(https://www.reddit.com/r/reactjs/comments/9lzm8g/react_switching_themes_css/),但我认为这不是一个好主意实践。有谁能够帮助我?
编辑:我尝试了reddit解决方案:现在它正在改变主题,但一瞬间我绝对可以看到一些我根本不喜欢的纯html东西。这是我的代码
import React, { useState } from "react";
import ReactDOM from "react-dom";
import Tabs from "./Tabs";
import "react-toastify/dist/ReactToastify.min.css";
import { Helmet } from "react-helmet"
export const Application = () =>{
const [theme, setTheme] = useState(true)
const changeThemeHandler = (event) => {
event.preventDefault();
setTheme(!theme)
}
return(
<>
<button onClick={(event) => changeThemeHandler(event)}>Change Theme</button>
<Helmet>
<link rel="stylesheet" href={`${process.env.PUBLIC_URL}/${theme ? 'lightbootstrap.min.css' : …Run Code Online (Sandbox Code Playgroud)