在 React 中如何实现亮色 / 暗色主题切换?

推荐「CSS 变量 + Context + 根节点 class」的方案,性能好、可动态换肤。

1. 用 CSS 变量定义两套主题

:root { --bg: #fff; --text: #111; }
:root.dark { --bg: #111; --text: #fff; }
.app { background: var(--bg); color: var(--text); }

2. 用 Context 管理主题状态并切换根节点 class

const ThemeContext = createContext()

function ThemeProvider({ children }) {
  const [theme, setTheme] = useState(
    () => localStorage.getItem('theme') || 'light'
  )

  useEffect(() => {
    document.documentElement.className = theme  // 切换到 <html class="dark">
    localStorage.setItem('theme', theme)
  }, [theme])

  return (
    <ThemeContext.Provider value={{
      theme,
      toggle: () => setTheme(t => t === 'light' ? 'dark' : 'light')
    }}>
      {children}
    </ThemeContext.Provider>
  )
}

其他方案

注意点

同分类其他题目