Created
August 10, 2021 15:17
-
-
Save chetanraj/15e7960d6acc11695b86b53113c7d3fa to your computer and use it in GitHub Desktop.
useThemeExample
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
function useTheme(theme) { | |
useLayoutEffect( | |
() => { | |
// Iterate through each value in theme object | |
for (const key in theme) { | |
// Update css variables in document's root element | |
document.documentElement.style.setProperty(`--${key}`, theme[key]); | |
} | |
}, | |
[theme] // Only call again if theme object reference changes | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment