Skip to content

Instantly share code, notes, and snippets.

@rimzzlabs
Last active September 26, 2022 07:33
Show Gist options
  • Save rimzzlabs/47049dbfac0fe48f29acbaae01aa46d5 to your computer and use it in GitHub Desktop.
Save rimzzlabs/47049dbfac0fe48f29acbaae01aa46d5 to your computer and use it in GitHub Desktop.
custom hook to manage theme
import { useCallback, useEffect, useState, } from 'react'
type Theme = 'dark' | 'light'
export default function useTheme(){
const [theme, setTheme] = useState<Theme>('light')
const changeTheme = useCallback((payload: Theme) => {
return () => {
setTheme(payload)
}
}, [])
useEffect(() => {
document.documentElement.className = theme
}, [theme])
return [theme, changeTheme]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment