I've made a simple utility function that should read given CSS variable using getPropertyValue function on :root element.
Code:
export const getCssVariable = (name: string): string => {
const value = window
.getComputedStyle(document.documentElement)
.getPropertyValue(name);
return value;
};
This works if used in useEffect hook, but outside of it, it returns an empty string.
- Why does this happen?
- Is there another way to read CSS variables in React? Maybe through custom hook?
Thanks.