mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
20 lines
529 B
JavaScript
20 lines
529 B
JavaScript
import { useContext, createContext, useState, useEffect } from 'react'
|
|
import localStorage from 'localStorage'
|
|
|
|
const ThemeContext = createContext()
|
|
|
|
export function ThemeProvider ({ children }) {
|
|
// 初始值
|
|
const defaultTheme = localStorage.getItem('theme')
|
|
const [theme, changeTheme] = useState()
|
|
useEffect(() => {
|
|
changeTheme(defaultTheme)
|
|
})
|
|
|
|
return (
|
|
<ThemeContext.Provider value={{ theme, changeTheme }}>{children}</ThemeContext.Provider>
|
|
)
|
|
}
|
|
|
|
export const useTheme = () => useContext(ThemeContext)
|