Last active
September 26, 2022 07:33
-
-
Save rimzzlabs/47049dbfac0fe48f29acbaae01aa46d5 to your computer and use it in GitHub Desktop.
custom hook to manage theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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